Vowel count
total_vowels = 0 #For each character in s for char in s: if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u': total_vowels +=1 print("Number of vowels: "+str(total_vowels))
Substring count
total = 0 for i in range(len(s)): if s[i:i+3]=="bob": total+=1 print("Number of times bob occurs is: "+ str(total))
Longest alphabetical substring
Enumerate
Nested ifs
s = 'ppzgyhplhevnxeeubykd' longest = "" temp= "" #Enumerate the string to reveal position of each character # For each character and position of character for pos, char in enumerate(s): # If the character is not in the first position if pos != 0: # And if the charafter is greater than the one before if char >= s[pos-1]: # Add character to temp if temp == '': temp += s[pos-1] temp += char # If the character is in the first position of the string, add it to the temp else: # Add the first character to temp if temp == '': temp += s[pos] # If temp is longer than the previous longest string, overwrite the longest string if len(temp)> len(longest): longest = temp if char < s[pos-1]: temp ='' print("Longest substring in alphabetical order is: "+longest)
Bisection number guessor
''' The program works as follows: you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search, the computer will guess the user's secret number! Is your secret number 91? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ''' high = 100 low = 0 uinput = '' preguess = 0 guess = 0 print("Please think of a number between 0 and 100!") #while loop while True: guess = int((high+low)/2) # if guess == preguess: break print("Is your secret number " + str(guess) + "?") # within a while condition being "h" or "l" loop: give prompt with string 'Is your secret number #?' and ask for input| uinput uinput = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.") print(uinput) if uinput == "h" or uinput == "l" or uinput == "c": #Calculate next using the tow points of reference to get next guess |guess preguess if uinput == "h":#if input is h: change highpoint to guess high = guess if uinput == "l":#if input is l: change lowpoint to guess low = guess if uinput == "c":#if input is l: break break else: print("Sorry, I did not understand your input.") continue preguess = guess print("Game over. Your secret number was: "+str(guess))#Tell user his number guess
Recursive bisection checker
""" Created on Wed Jun 20 12:48:03 2018 @author: Azam A We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you're looking for (the "test character"). If they are the same, we are done - we've found the character we're looking for! If they're not the same, check if the test character is "smaller" than the middle character. If so, we need only consider the lower half of the string; otherwise, we only consider the upper half of the string. (Note that you can compare characters using Python's < function.) Implement the function isIn(char, aStr) which implements the above idea recursively to test if char is in aStr. char will be a single character and aStr will be a string that is in alphabetical order. The function should return a boolean value. As you design the function, think very carefully about what the base cases should be. """ # a single character string and a alphabetized string going in # a boolean value comes out def isIn(char, aStr):#stub ''' char: a single character aStr: an alphabetized string returns: True if char is in aStr; False otherwise ''' if len(aStr) <= 1: return char == aStr elif aStr[int(len(aStr)/2)] > char: return isIn(char, aStr[0:int(len(aStr)/2)]) else: return isIn(char, aStr[int(len(aStr)/2):len(aStr)]) print(isIn("a","abhors"),#true isIn("b","abhors"),#true isIn("c","abhors"),#false isIn("q","abhors"),#false isIn("s","abhors"))#true
Program that appeases the dad
''' Write a Python function, evalQuadratic(a, b, c, x), that returns the value of the quadratic This function takes in four numbers and returns a single number. ''' #takes in four numbers and returns a single number. #returns value of the quadratic trinomial # #def evalQuadratic(a, b, c, x): #stub # return 0 ''' def evalQuadratic(a, b, c, x): return a*x**2+ b*x + c print(evalQuadratic(1, 0, 0, 2)== 1*2**2+ 0*2 + 0, evalQuadratic(0, 1, 0, 2)== 0*2**2+ 1*2 + 0, evalQuadratic(0, 0, 1, 2)== 0*2**2+ 0*2 + 1, evalQuadratic(8, 6, 1, 0)== 8*0**2+ 6*0 + 1)'''
HTML writer program that inserts heading and textarea
# -*- coding: utf-8 -*- """ Created on Tue Jul 17 13:09:16 2018 @author: Azam A """ userheading = input("enter heading:\n") usertext = input("enter text:\n") print("
"+userheading + "
") print("
\n" + usertext + ""+ "textarea>")