here are the notes in which I used, PLUS at the very bottom is the problem that took me the longest to solve, I bet the solution is way shorter than my response, but whoooo carrreesssss
# Looping over characters # Time to combine some of our skills. We want to use our understanding # of strings, the len function and while loops to print out each # character of a string, one per line ''' word = 'Grok' i = 0 while i < len(word): print(i, word[i]) i += 1 ''' # String methods # Since we're discussing strings, let's go over some more useful # string methods. We've seen how to call a method on a string object, (like upper) # and introduced a couple of string methods for converting # strings to uppercase and lowercase, and replacing bits of # a string with other strings. ''' s = 'hello world' print(s.startswith('hello')) print(s.endswith('rld')) ''' # Removing whitspace from around a string. ''' s = ' abc ' print(s.strip()) print(s.lstrip()) print(s.rstrip()) s = 'hello world' print(s.find('w')) print(s.find('x')) ''' # Finding factots - the problem # Let's look at a worked example using loops. We want to write a program # to find all the factors of 540. A factor of a number n is an int # that can be multiplied by another int to give n. For example, the factors # of 9 are; 1,2,4 and 8 since: ''' 1 x 8 = 8 2 x 4 = 8 ''' # And the factors of 24 are 1,2,3,4,6,8,12 and 24: ''' 1 x 24 = 24 2 x 12 = 24 3 x 8 = 24 4 x 6 = 24 ''' # One way of finding factors is to use your calculator and checking all the numbers less # than n one by one, This works well for small numbers (like 24) # but quicly becomes impractical for larger numbers (like 540). # Can you solve it faster using Python? # Finding factots - the algorithm # The first step in solving a programming problem is work out # the algorithm or method that we need to follow. # The simplest approuch is to mimic what you would do by hand # using your calculatr | Cheching each number one by one is called # brute force solution, Computers are so fast at doing basic calculations # that the brute force solution is often a good one for problems like this. # Let's write down the individual steps we need to follow: # 1. Choose the first number to test, start with 1: # 2. See if that number divided evenly into 540 using the modulus # operator, if it does print it out as a factor: # 3. Choose the next number by adding 1 to your last choice: # 4. If your number to test is greater than 540 then you have finished, # otherwise go back to step 2. # HA, i did it by myself wotofit ''' i = 1 x = 54324235435 while i <= x: if x % i == 0: print(i) i += 1 ''' # When a for loops is best # Every problem that can be solved using a while loop can also be # solved using a for loop (and vice versa) So how do you know # which one to use? # Generall, when you know how many iteratios you need ( either a set # number ot to iterate through a list) it is easier to use a for loop) # For example ''' colours = ['red', 'blue', 'green'] for i in colours: print(i) ''' # When a while loop is best # Generally, when you don't know how many iterations you need, # and you want to keep looping while some condition is true # a while loop is better, for example: ''' sum = 0 limit = 10 i = 0 while sum < limit: sum += i print(i, sum) i += 1 ''' # Storing multiple values # Imagine writing a program to store a ranking of your favourite # authors. Given what you know so far, you would use a seperate # variable for each author's name and mention each variable to print them ''' authors = ["J.K. Rowling", "P. Pullman", "S. Collins"] print(authors[0]) print(authors[1]) print(authors[2]) authors = ["J.K. Rowling", "P. Pullman", "S. Collins"] for author in authors: print(author) ''' ''' numbers = input('What are ur fave numbers: ') nums = numbers.split() for i in nums: print (i) ''' # Phonic tester ''' line = None while line != '': line = input('Line: ') if line == '': break phonic = line.split() start = phonic[0] start_4 = phonic [3] a = start[0] if start_4.startswith(a): print('Good!') else: print("That's not right!") ''' # Accessing list elements # List items can be accessed using indexing (a.k.a subscripting) ''' colours = ['red', 'blue', 'green', 'yellow'] print(colours[1]) print(colours[-1]) ''' # Modifying list elements # You may remember from earlier that strings are # Immutable, which means that you cannot change their value. # If you try to alter a character in a string, Python will # Complain with a type error! # Lists are different. List elements can be modified by assigning to # Subscripts: ''' colours = ['red', 'blue', 'green', 'yellow'] colours[0] = 'crimson' colours[-2] = 'lime' print(colours) ''' # Appending and sorting # There are various list methods that can be used to modify lists. # The append methos adds an item to the end of the list: ''' numbers = ['one', 'two', 'three'] numbers.append('four') print(numbers) ''' # The sort() method sorts the items in order, e.g. string in # alphabetical order and numbers in ascending order. ''' pets = ['dog','mouse','fish','cat'] pets.sort() print(pets) ''' # The reverse() method sorts the items in reverse order: ''' pets = ['dog','mouse','fish','cat'] pets.reverse() print(pets) ''' # Reversing a list # The reverse method reverses the order of the entire list. # Just like the string methods we saw last week, notice that calls # to list method name, seperated by a dot, e.g. pets.reverse() # Any other values the method needs to do its job is provided in a # normal way e.g 'cat' is given as an extra argument inside the round # brackets inn pets.append('cat') # Unlike methods applied to strings, notice that these methods modify # the original lists rather than creating new ones and so they don't return # a value. You can tell this because there is no output when you type them into # the interpreter, so we need to put pets on a separate line to see what happened # to our list. # Looping with append # Say we want to sort some books for the library catalogue. We ask the user which books they # are returning (one at a time), and then want to print them out in alphabetical order. ''' books = [] book = None while book != '': book = input('What book are you returning? ') books.append(book) books.sort() for i in books: print(i) ''' # List of characters and words # Lists can be constructed from strings using the list builtin function ''' print(list('hello')) ''' # Splitting on other characters # You can also split strings using other characters, or strings, as the seperator, For example # sometimes you may want to split a string using the colon character ':' ''' text = input('text: ') line = text.split(':') for i in line: print(i) ''' # Creating sublists from lists # Slicing also works like on strings to create a new sublist: ''' a = ['red','blue','green','yellow'] less = a[:2] print(less) ''' # This is useful for checking if a word is in a string: ''' words = ['the', 'fox', 'jumped', 'over', 'the', 'dog'] print('jumped' in words) ''' # Joinging a list of strings # Another useful string method is join which joins a list of strings back together using # a string as a seperator ''' sep = ':' val = ['a','b','c','d'] print(sep.join(val)) ''' # Splitting and joining # You'll often see a literal string (like the space ' ') used: ''' val = ['a','b','c','d'] print('.'.join(val)) ''' # This is handy when you want to remove duplicate spaces from use input. ''' msg = input('Enter your letters: ') letters = msg.split() print('.'.join(letters)) ''' # If the user enters a series of letters with extra spaces between them, # the program removes these spaces and prints them out with a single space separating each. # Checking if a word is alphabetical, reversed, or not in any order. ''' word = input('Word: ') word = word[0].lower() + word[1:].lower() al = list(word) al2 = al.sort() al3 = ''.join(al) al_2 = al.reverse() al_3 = ''.join(al) alpha = al3 revd = al_3 same = word[0] * len(word) if word == alpha: print('alphabetical') elif word == revd: print('reverse alphabetical') elif word != alpha or revd: print('not alphabetical in any way') ''' # Like Yoda, you speak # Yoda speaks in a bit of a strange way. Imagine you wanted to write a program to speak like # Yoda. Your program reads in four words from the user and moves them around to create a # sentence that sounded like Yoda # he is your father # --> # your father he is # Using a list structure makes this program quite easy to write. Lets write on that # reads in a single line of text and moves the words around using lists. Like always we first # break the problem down into smaller steps: # 1. Read the line of text # 2. Seperate the line into a list of two words # 3. Change the words to Yoda-speak order # 4. Print out the new text. ''' line = input('You say: ') yoda = line.split() yoda = yoda[2] + ' ' + yoda[3] + ' ' + yoda[0] + ' ' + yoda[1] print('Yoda says: ', yoda) ''' rule = [] broken = [] tb = 0 tr = 0 line = ' ' while line: lines = input('Line: ') line = lines.split() for word in line: if 'ie' in word: if 'cie' in word: tb += 1 elif word.count('cie') > 1: tb += 1 elif word.count('ie') > 1: tr += 1 elif 'ie' in word: tr += 1 if 'ei' in word: if 'cei' in word: tr += 1 elif word.count('cei') > 1: tr += 1 elif word.count('ei') > 1: tb += 1 elif 'ei' in word: tb += 1 print('Number of times the rule helped: {0}'.format(tr)) print('Number of times the rule was broken: {0}'.format(tb))