Greetings, Jedi Padawans, fellow coders, and esteemed Jedi Knights.
In the Star Wars universe, Jedi Knights are skilled in the use of the Force, a mystical energy that they can manipulate to achieve various abilities. In this coding challenge, we'll be simulating Jedi training by creating a program that generates a sequence of numbers and applies various transformations to them using...the power of code!
Instructions:
Write a program that generates a sequence of 10 random numbers between 1 and 10.
Use the Force (code) to apply the following transformations to the sequence:
- Multiply every even number by 2
- Add 5 to every odd number
- Replace any number greater than 10 with 1
- Print the final sequence to the console.
Hints:
- You can use the rand method in Ruby or the random module in Python to generate random numbers.
- You can use an if statement to check whether a number is even or odd.
- You can use a for loop to iterate over the sequence and apply the transformations.
The challenge begins. May the fourth be with you!
Top comments (8)
Assuming that any number greater than 10 gets replaced with 1, I wrote up a little script, created a repository for it, and hereby present github.com/rainleander/may-the-fourth
I also wrote up a little response post - I hope that's oke!
Hey everyone! I wanted to encourage all the newcomers to give this challenge a shot. Don't worry if you're not an experienced developer yet, challenges like these are a great way to dip your feet into the field and learn some new skills. And who knows, you might surprise yourself with what you can come up with!
For those of you who are more seasoned devs, why not try out some whacky solutions and see what you can come up with?
I'm personally introducing my own overly-engineered solution to get your noggings churning!
Behold the Fully Realized And Needlessly Komplex Extension-based Numerical Sequence Transformation Engine Involving a Number builder or FRANKENSTEIN.
Let's all have some fun and challenge ourselves in new ways. Good luck, and happy coding!
import random
sequence = [random.randint(1, 10) for _ in range(10)]
for i in range(len(sequence)):
if sequence[i] % 2 == 0:
sequence[i] *= 2
else:
sequence[i] += 5
if sequence[i] > 10:
sequence[i] = 1
print(sequence)
I had to golf it :P
8381 bytes:If anyone can go shorter I'd love to see it!
Your transformed sequence is wrong according to the transformations, since it contains numbers greater than 10, which would be removed by step 3.
TBH, your instructions are extremely ambiguous and - unless I'm missing something, your sample output is not achievable using any interpretation of the instructions I can think of. Step 3 is the main offender here.
Unless we are supposed to do 1 or 2 or 3 based upon the original number - in which case 3 is never going to happen. Overall, a pretty poorly worded challenge to give to beginners.
I think I got it right: github.com/mishmanners/JediTraining
Someone tell me 😄