Python Week 2
Jack Pordi,  13 February 2018

Python Week 2 Challenges

This week's challenges will be a little more difficult than last weeks', but that's to be expected. Once again, all the following can be done with just the content covered in lectures, and will test your knowledge of lists and loops. Remember, if you've got any questions, feel free to join our slack and ask mentors and others there.

#1: Printing n numbers (Very Easy)

  • Asks the user to input a number, then prints all the numbers from 0 to n.
  • Bonus: Have the program keep repeating itself, until the user inputs stop.

#2: Sum of numbers from 1 to n (Easy)

  • Again, asks the user for a number n.
  • Calculates the sum of all the numbers from 1 to n (inclusive). For example, the sum of numbers 1 to 10 should be 55, 1 to 100 should be 5050, and so on. Though you can simply use the formula for an arithmetic sequence, try to use either a while or a for loop so you can practice your knowledge of them.
  • Bonus: Instead of asking the user for just one number n, ask the user for two numbers a and b and find the sum of all the numbers between them instead.

#3: Summing up a user's list of numbers (Easy)

  • Asks the user for a number. Ask user again for another number. Keep going on until the user inputs sum, in which case, print the sum of all the numbers the user entered.

#4: Minimum number (Easy)

  • Same as #3, but instead stops at min, and prints out the smallest number that the user entered.
  • Bonus: Can you combine this with #3? e.g. if user inputs sum, then prints the sum, if the user inputs min, then prints the smallest number. Can you implement more of your own?

#5: Numbers Pyramid (Intermediate)

  • Asks the user for a number n (again! This is becoming a theme...).
  • Prints a 'number pyramid' of size/depth n. For example, with n = 9:
  • For this excercise, you're going to need to be able to print without starting a newline everytime. To do this, simply use a slightly modified version of print: print("Hello", end=''). You can also simply keep an accumulating string.

  • Hint: You might want to think about using loops within loops.

#6: Fibonacci (Hard)

  • Asks the user for a number n and prints out the nth fibonacci number. The nth fibonacci number is defined by the sum of the two previous fibonacci numbers. The 1st and 2nd numbers are 1. For example, the sequence goes like this: 1, 1, 2, 3, 5, 8, 13, 21, and so on.

#7: Sum of digits (Hard)

  • Similar to #2, however, only adds up numbers whose sum of digits is greater than 11. So, we would ignore 99. as 99 = 18, but 113 will be added as 1 + 1 + 3 = 5.
  • Hint: to find the sum of digits of a number, you might want to consider using the % and // operators, which gives the remainder and quotient of integer division.

#8: Checking a list (Intermediate)

  • One at a time, gets 10 numbers from user input and puts it into a list. Then, asks the user for another.
  • Prints either yes or no, depending on whether the last input number is included in the list of 10 numbers.
  • Note: If the wanted element is included in the list more than once, it should only print yes once.

#9: #3 and #4 using Lists

  • Combine #3 and #4 together and reimplement it using a list to keep track of numbers. Consider adding more functionality, such as multiply and/or max.