4) d. Range Function Examples




You are trapped by some crazy mathematicians, who loves to play Boltz. The game is the following: you have to count up from one and if a number is divisible by 3 or 7 you have to say, Boltz. They said if you make a mistake they will torture you.
Write a program that iterates through all the numbers until 100 and prints either the number or Boltz.

1
2
Output:
['Boltz', 1, 2, 'Boltz', 4, 5, 'Boltz', 'Boltz', 8, 'Boltz', 10, 11, 'Boltz', 13, 'Boltz', 'Boltz', 16, 17, 'Boltz', 19, 20, 'Boltz', 22, 23, 'Boltz', 25, 26, 'Boltz', 'Boltz', 29, 'Boltz', 31, 32, 'Boltz', 34, 'Boltz', 'Boltz', 37, 38, 'Boltz', 40, 41, 'Boltz', 43, 44, 'Boltz', 46, 47, 'Boltz', 'Boltz', 50, 'Boltz', 52, 53, 'Boltz', 55, 'Boltz', 'Boltz', 58, 59, 'Boltz', 61, 62, 'Boltz', 64, 65, 'Boltz', 67, 68, 'Boltz', 'Boltz', 71, 'Boltz', 73, 74, 'Boltz', 76, 'Boltz', 'Boltz', 79, 80, 'Boltz', 82, 83, 'Boltz', 85, 86, 'Boltz', 88, 89, 'Boltz', 'Boltz', 92, 'Boltz', 94, 95, 'Boltz', 97, 'Boltz', 'Boltz']




You have been hiding because of a series of murders happened in your building. Your neighbor calls you to his room but when you get there all that you see there is an old PC with a file.
In the monitor you also see two numbers, 4 and 7. You are wondering about their significance, so you need to create a program that prints the list’s contents between lines 4 and 7. Don’t forget, that your neighbor wanted to communicate something important and your life might depend upon it!
Since you haven’t learnt file handling at this stage, assume that you quickly read the file’s contents into a list. Work on the list.

1
2
3
4
Output:
You are in danger
You have to hide
He wants to get you




Did you know? The range function need not return consecutive numbers. Instead, you can specify the gap between each number as a third parameter! For example: range(starting index, ending index, width or gap between each number) .

Humanity is in the middle of an alien invasion. Your team managed to hack the enemy’s communication, but you are unable to read their messages because they are coded. To decode them you need to read only every second character. You have to write a program that iterates through a message and prints every second character. Hurry up, humanity is depending on you.

1
2
Output:
do not worry we come in peace




You are closed into a room with a door key that doesn’t work. You have received a helpful message from your alien friend. You have to translate to English text from an Andromedian language, which is similar to English, just backward. You have to translate a 100 line document to open the door. Write a program that iterates through the text from the ending until the start and prints it. Hurry up, because you might run out of oxygen in a few minutes.

1
2
Output:
Insert the key inside out to open




You are trapped in your ex’s room. The door is locked. You have found a lot of keys, and you need to find the right one. Every key has a sticky with a number. You have to find the right key.
You have also found a note that mentions that the correct key number lies in an series of every third number starting from 5 until 67.
You have 10 minutes to find the right key. Write a program that quickly generates the series of numbers for you and also checks whether any of the key number lies in it. (Use the range function to generate the series).

1
2
Output:
Found the key! It's 35




You are in the annual meeting of fanatic mathematicians. They told you that you need to find out the sequence they have in their mind. They helped you with the following information: the sequence is between 1 and 85. Find out what sequence they have in mind (Assume that it’s given to you in a variable but you cannot use it directly). Don’t forget that they are fanatic, so they might hurt you if you fail the test.

1
2
3
Output:
The sequence on their mind is:  [1, 38, 75]
The range function used is: range(1, 85, 37)




Some of your programmer friends love to play Sudoku but they are unable to create a program that can solve a game. You want to fit into the group so you have to create one. Create a program that finds a number for a given row. Use range to iterate through indexes.

1
2
3
4
5
6
7
Output:
3 not in row
4 not in row
4 not in column
5 not in row
5 not in column
5 is ok