├── Week5- Object Oriented Programming └── Nothing.txt ├── Week3- Structured Types ├── Lecture5- Tuples and Lists │ └── Nothing.txt └── Lecture6- Dictionaries │ └── Fibonnaci.txt ├── Week4- Good Programming Practices ├── Lecture7- Debugging │ └── Nothing.txt └── Lecture8- Exceptions and Assertions │ ├── Finding an Even Number.py │ └── Summing Digits in a String.py ├── Week6- Algorithmic Complexity ├── Lecture11- Computational Complexity │ └── Nothing.txt └── Lecture12- Searching and Sorting Algorithms │ └── Why Add 1 to Mid.txt ├── Week2- Simple Programs ├── Lecture3- Simple Algorithms │ ├── Decimal Equivalent of Binary (Chap 3.4).txt │ ├── What would happen to the Bisection Search Code (Chap 3.3 #1).txt │ ├── Extending Algorithm to Cube Roots (Chap 3.3 #2).txt │ ├── Counting Newton Rasphon Iterations (Chap 3.4).py │ └── Root and Power (Chap 3.1).py └── Lecture4- Functions │ └── Function isIn (Chap 4.1.1).py ├── README.md └── Week1- Python Basics ├── Lecture2- Core Elements of Programs ├── Adding up Decimals(Chap 3.2).py ├── Print Letter X multiple times using while loop(Chap 2.4 #1).py ├── Largest Odd Number out of 10 Numbers(Chap 2.4 #2).py └── Largest Odd Number(Chap 2.2).py └── Lecture1- Introduction to Python └── Writing an Algorithm for a person - Executing it using a computer(Chap 1).txt /Week5- Object Oriented Programming/Nothing.txt: -------------------------------------------------------------------------------- 1 | Nothing here! 2 | -------------------------------------------------------------------------------- /Week3- Structured Types/Lecture5- Tuples and Lists/Nothing.txt: -------------------------------------------------------------------------------- 1 | Nothing here! 2 | -------------------------------------------------------------------------------- /Week4- Good Programming Practices/Lecture7- Debugging/Nothing.txt: -------------------------------------------------------------------------------- 1 | Nothing here! 2 | -------------------------------------------------------------------------------- /Week6- Algorithmic Complexity/Lecture11- Computational Complexity/Nothing.txt: -------------------------------------------------------------------------------- 1 | Nothing here! 2 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture3- Simple Algorithms/Decimal Equivalent of Binary (Chap 3.4).txt: -------------------------------------------------------------------------------- 1 | @author: ali_shehzad 2 | 3 | Finger exercise 9: What is the decimal equivalent of the binary number 10011? 4 | Ans: 1*16 + 0*8 + 0*4 + 1*2 + 1*1 = 19 5 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture3- Simple Algorithms/What would happen to the Bisection Search Code (Chap 3.3 #1).txt: -------------------------------------------------------------------------------- 1 | @author: ali_shehzad 2 | 3 | Finger exercise 7: What would the code in Figure 3.4 do if the statement x = 25 were replaced by x = -25? 4 | Ans: The code would get stuck in an infinite loop. The ans would get closer and closer to 0. 5 | -------------------------------------------------------------------------------- /Week3- Structured Types/Lecture6- Dictionaries/Fibonnaci.txt: -------------------------------------------------------------------------------- 1 | @author: ali_shah_shehzad 2 | 3 | Finger exercise 12: When the implementation of fib in Figure 4.7 is used to compute fib(5), how many times does it compute the value of fib(2) on the way to 4 | computing fib(5)? 5 | 6 | Answer: In the definition of factR, add the following command between the else clause, and return fib(n-1) + fib(n-2) statement. 7 | print("Computing fib of", n-1, "and", n-2) 8 | Run the program. You will now see that fib(2) is computed 3 times and hopefully understand how. 9 | -------------------------------------------------------------------------------- /Week4- Good Programming Practices/Lecture8- Exceptions and Assertions/Finding an Even Number.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | @author: ali_shehzad 4 | """ 5 | 6 | """Finger Exercise 13: Implement a function that satisfies the specification.""" 7 | 8 | def findAnEven(L): 9 | """Assumes L is a list of integers 10 | Returns the first even number in L 11 | Raises Value error if L does not contain an even number.""" 12 | 13 | for num in L: 14 | if num%2 == 0: 15 | return num 16 | raise ValueError("No even number in the list.") 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MIT6.00.1x 2 | Finger Exercises(Introduction to Computation and Programming using Python by John V Guttag) 3 | 4 | Hi Coders! 5 | This repository will contain solutions to all the Finger Exercises related to MIT6.00.1x from the book Introduction to Computation and Programming using Python by John V Guttag. 6 | Please note that more elegant solutions do exist than the one's I've provided but I've solved them using what has been taught in the book or the lectures so far. If you have more efficient solutions which do satisfy these requirements, please feel free to edit. 7 | 8 | email: alishehzad3.142@gmail.com 9 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture3- Simple Algorithms/Extending Algorithm to Cube Roots (Chap 3.3 #2).txt: -------------------------------------------------------------------------------- 1 | @author: ali_shehzad 2 | 3 | Finger exercise 8: What would have to be changed to make the code in Figure 3.4 work for finding an approximation to the cube root of both negative 4 | and positive numbers? (Hint: think about changing low to ensure that the answer lies within the region being searched.) 5 | 6 | 1. Change “**2” everywhere in the code to “**3”. 7 | 2. First check if x is positive or negative. If x is negative set high = 0 and low = x. If x is positive, follow the same procedure as with finding square roots. 8 | -------------------------------------------------------------------------------- /Week1- Python Basics/Lecture2- Core Elements of Programs/Adding up Decimals(Chap 3.2).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Sep 15 03:10:48 2017 4 | 5 | @author: ali_shehzad 6 | """ 7 | 8 | """ 9 | Finger exercise 5: 10 | Let s be a string that contains a sequence of decimal numbers separated by commas, 11 | e.g., s = '1.23,2.4,3.123'. Write a program that prints the sum of the numbers in s. 12 | """ 13 | 14 | 15 | total = 0 16 | s = input("Please enter a string of sequence of decimal numbers: ") 17 | for c in s.split(","): #split 's' creating a list with each decimal number as it's element 18 | total += float(c) 19 | print(total) 20 | -------------------------------------------------------------------------------- /Week4- Good Programming Practices/Lecture8- Exceptions and Assertions/Summing Digits in a String.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | @author: ali_shehzad 4 | """ 5 | 6 | """Finger exercise 13: Implement a function that meets the specification below. 7 | Use a try-except block.""" 8 | 9 | 10 | def sumDigits(s): 11 | """Assumes s is a string 12 | Returns the sum of the decimal digits in s 13 | For example is s is 'a2b3c' it returns 5""" 14 | total = 0 15 | for i in s: 16 | try: 17 | total += int(i) 18 | print(total) 19 | except ValueError: 20 | print("Ignoring exception") 21 | return total 22 | -------------------------------------------------------------------------------- /Week1- Python Basics/Lecture2- Core Elements of Programs/Print Letter X multiple times using while loop(Chap 2.4 #1).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Sep 15 03:10:48 2017 4 | 5 | @author: ali_shehzad 6 | """ 7 | 8 | """ 9 | Finger exercise 3: 10 | Replace the comment in the following code with a while loop. 11 | numXs = int(input('How many times should I print the letter X? ')) 12 | toPrint = '' 13 | concatenate X to toPrint numXs times 14 | print(toPrint) 15 | """ 16 | 17 | numXs=int(input("How many times should I print the letter X? ")) 18 | toPrint="" 19 | itersLeft=numXs 20 | while itersLeft != 0: 21 | toPrint += 'X' 22 | itersLeft -= 1 23 | print(toPrint) 24 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture4- Functions/Function isIn (Chap 4.1.1).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | @author: ali_shehzad 4 | """ 5 | 6 | """ 7 | Finger exercise 11: Write a function isIn that accepts two strings as 8 | arguments and returns True if either string occurs anywhere in the other, 9 | and False otherwise. Hint: you might want to use the built-in str 10 | operation in. 11 | """ 12 | 13 | 14 | def isIn(str1, str2): 15 | if len(str1) > len(str2): #We're comparing which string is longer and then checking to see 16 | if str2 in str1: #if the shorter string is present in the longer string 17 | return True 18 | else: 19 | if str1 in str2: 20 | return True 21 | return False 22 | -------------------------------------------------------------------------------- /Week6- Algorithmic Complexity/Lecture12- Searching and Sorting Algorithms/Why Add 1 to Mid.txt: -------------------------------------------------------------------------------- 1 | @author: ali_shehzad 2 | 3 | Finger Exercise 15: Why does the code use mid+1 rather than mid in the second recursive call? 4 | 5 | •To bring the decrementing function closer to 0 as ‘mid’ has already been searched. 6 | •If we do not add 1 to mid when before passing it as a parameter to ‘low’, the function might run forever in some cases. 7 | Try simulating bSearch(L, 4, 3, 4) and see for yourself or look below. 8 | •Every time bSearch is called with these arguments; the value ‘3’ gets assigned to ‘mid’ because of integer division. 9 | Now mid has the same value as low and this value is passed again and again to the recursive function bSearch making it interminable. 10 | -------------------------------------------------------------------------------- /Week1- Python Basics/Lecture2- Core Elements of Programs/Largest Odd Number out of 10 Numbers(Chap 2.4 #2).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Sep 15 03:10:48 2017 4 | 5 | @author: ali_shehzad 6 | """ 7 | 8 | """ 9 | Finger exercise 4: 10 | Write a program that asks the user to input 10 integers, and then prints the largest 11 | odd number that was entered. If no odd number was entered, it should print a message to that effect. 12 | """ 13 | 14 | largestNum = 0 15 | itersLeft = 10 16 | 17 | while itersLeft != 0: 18 | num = int(input("Please enter an integer: ")) 19 | itersLeft -= 1 20 | if num%2 != 0 and num > largestNum: #compare only if number is odd 21 | largestNum = num 22 | 23 | if largestNum == 0: 24 | print("No odd number entered") 25 | else: 26 | print("Largest odd number is " + str(largestNum) + ".") 27 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture3- Simple Algorithms/Counting Newton Rasphon Iterations (Chap 3.4).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | @author: ali_shehzad 4 | """ 5 | 6 | #Newton-Rasphon for square root 7 | #Find x such that x**2 - 24 is within epsilon of 0 8 | 9 | """ 10 | Finger exercise 10: Add some code to the implementation of Newton-Raphson that keeps track of the 11 | number of iterations used to find the root. Use that code as part of a program that compares the 12 | efficiency of Newton-Raphson and bisection search. (You should discover that Newton-Raphson is 13 | more efficient.) 14 | """ 15 | 16 | epsilon = 0.01 17 | k = 123456789 18 | guess = k/2.0 19 | iteration = 0 20 | 21 | while abs(guess**2 - k) >= epsilon: 22 | guess = guess - (((guess**2) - k)/(2*guess)) 23 | iteration += 1 24 | print("Iternation no: " + str(iteration)) 25 | 26 | print("Square root of", k, "is about", guess) 27 | -------------------------------------------------------------------------------- /Week1- Python Basics/Lecture1- Introduction to Python/Writing an Algorithm for a person - Executing it using a computer(Chap 1).txt: -------------------------------------------------------------------------------- 1 | @author: ali_shehzad 2 | 3 | Finger exercise 1: 4 | Computers can be annoyingly literal. If you don’t tell them exactly what you want 5 | them to do, they are likely to do the wrong thing. Try writing an algorithm for driving between two 6 | destinations. Write it the way you would for a person, and then imagine what would happen if that 7 | person were as stupid as a computer, and executed the algorithm exactly as written. How many traffic 8 | tickets might that person get? 9 | 10 | 11 | Algorithm for reaching Department of Physics from The Great Dome (for a friend) 12 | 1. Head Northwest towards Vassar St for 90 meters. 13 | 2. Turn right and follow the road for 160 meters. 14 | 3. Turn right again and follow the road for 100 meters. 15 | 4. Turn left and follow the road for another 160 meters. 16 | 5. Turn right and you’re now on Ames St. 17 | 6. Make a final right turn and drive another 90 meters. 18 | 7. Destination is now on your right. 19 | 20 | What would happen if I tried executing it on a computer? 21 | If this Algorithm were to be fed into a computer, the autonomous vehicle would break all traffic signals, not slow down over speed bumpers, 22 | run over people and smash any vehicles/objects in between the journey. My driving license would be rescinded. 23 | -------------------------------------------------------------------------------- /Week2- Simple Programs/Lecture3- Simple Algorithms/Root and Power (Chap 3.1).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | @author: ali_shehzad 4 | """ 5 | 6 | """ 7 | Finger exercise 6: Write a program that asks the user to enter an integer and prints two integers, root 8 | and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no 9 | such pair of integers exists, it should print a message to that effect. 10 | 11 | Note: This question seems to be a bit flawed as integer**1 would always suffice the condition, 12 | therefore I am slightly changing the range for power to be 1 < pwr < 6. Lower powers for root are given 13 | priority ie (for integer = 16, the answer will be 4**2 instead of 2**4). I'm also assuming that then 14 | number entered is greater than 1, because one will always have a root for each power. 15 | """ 16 | 17 | integer = int(input("Please enter an integer greater than 1: ")) 18 | isFound = False 19 | if integer < 0: 20 | isNeg = True 21 | for pwr in range(2, 6): 22 | for root in range(2, abs(integer)+1): 23 | if root**pwr >= abs(integer): 24 | break 25 | #Now we'll compare root**power with integer if the power is even 26 | #or root**power with abs(integer) if power is ood and later replace the sign of root 27 | if root**pwr == integer or (root**pwr == abs(integer) and pwr%2 != 0): 28 | isFound = True 29 | break 30 | if isFound == False: 31 | print("No root/pwr pair exists for this intger between the given range.") 32 | else: 33 | if integer < 0: 34 | root = -root 35 | print(str(root) + "**" + str(pwr) + " = " + str(integer)) 36 | 37 | 38 | -------------------------------------------------------------------------------- /Week1- Python Basics/Lecture2- Core Elements of Programs/Largest Odd Number(Chap 2.2).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Sep 15 03:10:48 2017 4 | 5 | @author: ali_shehzad 6 | """ 7 | 8 | """Finger exercise 2: 9 | Write a program that examines three variables—x, y, and z—and prints the largest 10 | odd number among them. If none of them are odd, it should print a message to that effect 11 | """ 12 | 13 | 14 | print("Input 3 numbers and we'll output the largest of those odd numbers") 15 | 16 | x=int(input("Num1: ")) 17 | y=int(input("Num2: ")) 18 | z=int(input("Num3: ")) 19 | 20 | if x%2==0 and y%2==0 and z%2==0: #check if all of the numbers provided are even. 21 | print("No odd numbers provided.") 22 | elif x%2==0 and y%2==0: #check if x and y are even. 23 | print(z, "is the largest odd number.") #if above statement is true, z is the only odd num hence it's the largest. 24 | elif x%2==0 and z%2==0: #check if x and z are even. 25 | print(y, "is the largest odd number.") #if above statement is true, y is the only odd num hence it's the largest. 26 | elif y%2==0 and z%2==0: #check if y and z are even. 27 | print(x, "is the largest odd number.") #if above statement is true, x is the only odd num hence it's the largest. 28 | elif x%2==0: #check if x is even 29 | if y>z: #if above statement is true, we only have to compare y and z now. 30 | print(y, "is the largest odd number.") 31 | else: 32 | print(z, "is the largest odd number.") 33 | elif y%2==0: #check if y is even 34 | if x>z: #if above statement is true, we only have to compare x and z now. 35 | print(x, "is the largest odd number.") 36 | else: 37 | print(z, "is the largest odd number.") 38 | elif z%2==0: #check if z is even 39 | if x>y: #if above statement is true, we only have to compare x and y now. 40 | print(x, "is the largest odd number.") 41 | else: 42 | print(y, "is the largest odd number.") 43 | else: #all 3 numbers are odd so we'll compare all 3 of them 44 | if x>y and x>z: 45 | print(x, "is the largest odd number.") 46 | elif y>z: 47 | print(y, "is the largest odd number.") 48 | else: 49 | print(z, "is the largest odd number.") 50 | 51 | 52 | --------------------------------------------------------------------------------