Malleoz

Hi Iyce

Apr 23rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. # Q1a) Ask the user to enter a number n. Write a function to the sum of this series up to n terms.
  2. from math import pow # The series they are talking about is a geometric series, in which the previous term is doubled. So, we can represent the next term by taking 2^(x) where x is the xth term in the series, starting with 0.
  3. n = int(input("Enter a number: "))
  4. total = 0
  5. for x in range(0, n): # This loops goes through n terms starting at 0.
  6.     if x==n-1:
  7.         print(int(pow(2,x))) # This prints the last term without the plus sign.
  8.     else:
  9.         print(int(pow(2,x)), end="+") # This prints the current term followed by a plus sign for terms in the middle.
  10.     total+= pow(2,x) # This updates the total.
  11. print("Sum:", int(total)) # Prints the sum.
  12.  
  13. # Q1b) Ask the user to input two lists of equal size. Create a new list which interleaves the two lists and output it.
  14. string1 = input("Please enter list 1, separated by commas: ") # First takes input as a string.
  15. list1 = string1.split(',') # Converts first string into first list.
  16. string2 = input("Please enter list 2, separated by commas: ") # Second string.
  17. list2 = string2.split(',') # Converts to second list.
  18. while (len(list2) != len(list1)): # Checks to see if lists are same length. If not, re-enter list 2.
  19.     print("Lists are not same sizes!")
  20.     string2 = input("Please enter list 2, separated by commas: ")
  21.     list2 = string2.split(',')
  22. list3 = []
  23. for x in range(0,len(list1)): # Goes through each index in the lists.
  24.     list3.append(list1[x]) # First appends the element in the first list.
  25.     list3.append(list2[x]) # Then the element in the second list.
  26. print("The combined list is:", ", ".join(list3)) # This join function takes away the brackets when printing the list and allows you to add a comma after each element in the list.
  27.  
  28. # Q2) Ask the user to enter a number n. Output the illustrated pattern depending on n as shown below.
  29. n = int(input("Please enter a number n: "))
  30. for x in range(int((n/2)), 0, -1): # Loops through each pattern line.
  31.     print("*" * (x), end="")
  32.     print("o" * (n-2*x), end="")
  33.     print("*" * (x), end="")
  34.     print()
  35. print("o" * n)
  36. for x in range (1, int((n/2)+1)): # Loops through each pattern line.
  37.     print("*" * (x), end="")
  38.     print("o" * (n-2*x), end="")
  39.     print("*" * (x), end="")
  40.  
  41. # Q3) Ask the user to enter a sequence of numbers to sort. Then ask them to indicate which positions they want to not sort. Make a new list that sorts while keeping unsorted the specified ones.
  42. # Slightly buggy...Must choose positions of unsorted numbers such that the actual numbers themselves ascend.
  43. # Function to do insertion sort
  44. stringunsorted = input("Please enter the numbers you want to sort, separated by commas: ") # Takes in the numbers.
  45. list = stringunsorted.split(',') # Converts to a list.
  46. posDoNotSort = input("Please enter the positions of the numbers you do not want to sort: ") # Takes what positions we don't want to sort.
  47. posDoNotSort = posDoNotSort.split(',') # Converts to list
  48. sortedposDoNotSort = sorted(posDoNotSort, key=int) # Creates a new list that orders the unsorted positions ascending.
  49. keepTheseNumbersStill = []
  50. for x in range(0, len(list)):
  51.     try:
  52.         if (list[int(sortedposDoNotSort[x])-1]):
  53.             keepTheseNumbersStill.append(list[int(sortedposDoNotSort[x])-1])
  54.     except:
  55.         keepTheseNumbersStill.append(' ')
  56. sortedlist = sorted(list, key=int)
  57. print(sortedlist)
  58. for x in range(0, len(sortedlist)):
  59.     if keepTheseNumbersStill[x] != ' ':
  60.         sortedlist.remove(keepTheseNumbersStill[x])
  61.         sortedlist.insert(int(sortedposDoNotSort[x])-1, keepTheseNumbersStill[x])
  62. print("Here is your modified sorted sequence:",sortedlist)
Advertisement
Add Comment
Please, Sign In to add comment