Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Q1a) Ask the user to enter a number n. Write a function to the sum of this series up to n terms.
- 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.
- n = int(input("Enter a number: "))
- total = 0
- for x in range(0, n): # This loops goes through n terms starting at 0.
- if x==n-1:
- print(int(pow(2,x))) # This prints the last term without the plus sign.
- else:
- print(int(pow(2,x)), end="+") # This prints the current term followed by a plus sign for terms in the middle.
- total+= pow(2,x) # This updates the total.
- print("Sum:", int(total)) # Prints the sum.
- # Q1b) Ask the user to input two lists of equal size. Create a new list which interleaves the two lists and output it.
- string1 = input("Please enter list 1, separated by commas: ") # First takes input as a string.
- list1 = string1.split(',') # Converts first string into first list.
- string2 = input("Please enter list 2, separated by commas: ") # Second string.
- list2 = string2.split(',') # Converts to second list.
- while (len(list2) != len(list1)): # Checks to see if lists are same length. If not, re-enter list 2.
- print("Lists are not same sizes!")
- string2 = input("Please enter list 2, separated by commas: ")
- list2 = string2.split(',')
- list3 = []
- for x in range(0,len(list1)): # Goes through each index in the lists.
- list3.append(list1[x]) # First appends the element in the first list.
- list3.append(list2[x]) # Then the element in the second list.
- 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.
- # Q2) Ask the user to enter a number n. Output the illustrated pattern depending on n as shown below.
- n = int(input("Please enter a number n: "))
- for x in range(int((n/2)), 0, -1): # Loops through each pattern line.
- print("*" * (x), end="")
- print("o" * (n-2*x), end="")
- print("*" * (x), end="")
- print()
- print("o" * n)
- for x in range (1, int((n/2)+1)): # Loops through each pattern line.
- print("*" * (x), end="")
- print("o" * (n-2*x), end="")
- print("*" * (x), end="")
- # 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.
- # Slightly buggy...Must choose positions of unsorted numbers such that the actual numbers themselves ascend.
- # Function to do insertion sort
- stringunsorted = input("Please enter the numbers you want to sort, separated by commas: ") # Takes in the numbers.
- list = stringunsorted.split(',') # Converts to a list.
- posDoNotSort = input("Please enter the positions of the numbers you do not want to sort: ") # Takes what positions we don't want to sort.
- posDoNotSort = posDoNotSort.split(',') # Converts to list
- sortedposDoNotSort = sorted(posDoNotSort, key=int) # Creates a new list that orders the unsorted positions ascending.
- keepTheseNumbersStill = []
- for x in range(0, len(list)):
- try:
- if (list[int(sortedposDoNotSort[x])-1]):
- keepTheseNumbersStill.append(list[int(sortedposDoNotSort[x])-1])
- except:
- keepTheseNumbersStill.append(' ')
- sortedlist = sorted(list, key=int)
- print(sortedlist)
- for x in range(0, len(sortedlist)):
- if keepTheseNumbersStill[x] != ' ':
- sortedlist.remove(keepTheseNumbersStill[x])
- sortedlist.insert(int(sortedposDoNotSort[x])-1, keepTheseNumbersStill[x])
- print("Here is your modified sorted sequence:",sortedlist)
Advertisement
Add Comment
Please, Sign In to add comment