Iterations Exercises#

Section Title: Iterations Exercises

Question-1#

Print the letters with negative even indexes (-2, -4, …) from the given text using:

  • a for loop

  • a while loop

  • slicing with a step. (use join method())

text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  

Print the letters in a single line and comma separated.

Sample Output:
text : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
for loop : Y,W,U,S,Q,O,M,K,I,G,E,C,A,y,w,u,s,q,o,m,k,i,g,e,c,a
while loop: Y,W,U,S,Q,O,M,K,I,G,E,C,A,y,w,u,s,q,o,m,k,i,g,e,c,a
slicing : Y,W,U,S,Q,O,M,K,I,G,E,C,A,y,w,u,s,q,o,m,k,i,g,e,c,a

Question-2#

Find the sum of the first \(100\) terms of the following sequence:

\(\displaystyle \frac{1}{1^3+1^2}, \frac{1}{2^3+2^2}, \frac{1}{3^3+3^2}, \frac{1}{4^3+4^2}, ...\)

  • Use a for loop.

  • Use a while loop.

Question-3#

Ask the user to input a name and then insert a random punctuation mark between each character of the name.

  • Ensure that the output does not have a punctuation mark at the end.

Sample Output
Please enter a name : ashley
Randomly generated name: a&s#h”l#e”y

Question-4#

Ask the user to input a number and print the consecutive pairs of digits within this number.

  • Example: The consecutive digits in 156319672 are 56 and 67.

Sample Output
Please enter a number: 156319672
Consecutive pairs are: 56,67

Question-5: Secret Number Game#

This is another version of the Guess the Secret Number Game.

  • The user selects a secret 3-digit number.

  • The computer randomly selects a 3-digit number to find the secret number.

  • You can use random.randint or numpy.randint.

  • Count the number of attempts by the computer.

  • If the computer can find the secret number in fewer than 100 attempts, it wins; otherwise, it loses the game.

  • After each attempt, print the computer’s guess.

  • After each attempt, provide a hint to the computer whether the secret number is larger or smaller than the guess. The computer makes the next guess based on the hint

Sample Output
Choose a 3 digit secret number: 123
Try Number: 0 Guess: 654 is LARGE. Try Again!
Try Number: 1 Guess: 638 is LARGE. Try Again!
Try Number: 2 Guess: 467 is LARGE. Try Again!
COMPUTER WINS!
Number of tries: 3

Question-6: Scatter Plot#

Use a for loop to randomly select 100 points on a coordinate plane and plot them individually as a scatter plot. For each point:

  • The x and y coordinates are randomly chosen between 0 and 1.

  • The size of the point is a random number between 10 and 500.

  • The color is chosen from the characters of he following string: color_str = ‘rgbky’.

    • ‘r’ represents red, ‘g’ represents green,’b’ represents blue, ‘k’ represents black,’y’ represents yellow

  • The transparancey is represented by a random number between 0 and 1.

Refer to the scatterplot section in Appendix: Visualization for implementation guidance.

Question-7: Empirical Probability#

Flip a coin 100 times and count the number of heads.

  • Use the string ‘TH’ to randomly select either ‘T’ (tail) or ‘H’ (head).

    • Randomly choose 0 or 1 as the index of the character that will be selected randomly.

Question-8: Rotating Stick#

Use a for loop and the sleep method from the time library to sequentially display the rotation of ‘’, ‘–’, and ‘/’ characters 20 times.

  • Add a waiting time between each character to simulate the rotation of a stick.

  • Ensure that after printing each character, it is deleted before the next one is printed to maintain the rotating effect.

Question-9: Counter#

Use a for loop and the sleep method from the time library to display a counter counting from 0 to 999.

  • Ensure that one-digit numbers have two zeros added to the left, and two-digit numbers have one zero added to the left.

  • After printing each number, delete it before the next one is printed to maintain the rotating effect.

  • Numbers: 001, 002,…,010,011,…,999