1 / 26

Largest of x random numbers

Largest of x random numbers. Write a function that takes 2 integers (x and y) as input parameters , and generates x random numbers between 0 and y. It finds the largest of those random numbers. def find_largest ( x,y ): largest = 0; count = 0; while count < x:

kirra
Download Presentation

Largest of x random numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Largest of x random numbers • Write a function that takes 2 integers (x and y) as input parameters , and generates x random numbers between 0 and y. It finds the largest of those random numbers. deffind_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))

  2. Try: Multiplication tables • def func(x): • count = 0 • while (count <= 10): • print(count*x) • count += 1 • return (total) • print(func(5)) • def fancyfunc(x): • count = 0 • while (count <= 10): • print(str(count)+"x"+str(x)+"="+str(count*x)) • count += 1 • return (total) • print(fancyfunc(7)) • Write a function that takes as an input parameter a number, and then prints out that number's multiplication table up through 10, • e.g., if the number was 4, you'd get 0 4 8 12 … 40 (Or Fancier version): 0x4=0 1x4=4 2x4=8 3x4=12 … 10x4=40

  3. Average: count • Write a function that takes no inputs. It generates 1000 random numbers between -100 and 100. It counts how many of the random numbers are below 0 and how many are at or above 0. It then tells you which is more likely, below 0 or at or above 0. • Starting condition? • What makes the loop stop? • What inside the loop changes so that the loop will stop? from random import * def beloworabove(): count = 0 below = 0 above = 0 while (count < 1000): rand = randrange(-100,100) if (rand < 0): below += 1 else: above += 1 count += 1 if below > above: return("generated " + str(below) + " numbers below 0") else: return ("generated " + str(below) + " numbers above 0") print(beloworabove())

  4. What do we get? from math import * def f(a,b): n = min(a,b) i = 1 g = 1 while (i <= n): if (a%i == 0) and (b%i == 0): g = i i = i+1 return(g) print(f(32,24)) print(f(13,8)) print(f(24,16))

  5. For loop: another type of loop • We use For loops when we know exactly how many times the loop will occur • Form: for variablein [value1, value2,value3…lastvalue]: calculations • Example: def f(): for x in [1,2,3,4,5]: print(x) return(x) print(f())

  6. More for loops: def f(): for x in [1,3,5,7,9]: print(x) return(x) print(f()) def f(): for x in [2,7,1,9,4]: print(x) return(x) print(f())

  7. More for loops: def f(): y = 0 ct = 0 for x in [3.2, 7.1, 8.0, 3.4, 5.1]: print("including " + str(x)) ct +=1 y = y + x return(y/ct) print(f())

  8. Loops with strings: def f(y): ct = 0 for x in ["puppy","bunny","puppy","bird","echidna","puppy"]: if x == y: ct += 1 return(ct) print(f("puppy"))

  9. More for loops: def f(): for x in [0,1,2,3,4]: print(x) return(x) print(f()) • Shortcut: using range def f(): for x in range(5): # range(5) = [0,1,2,3,4] print(x) return(x) print(f())

  10. Same? def whilefunc(y): count = 0 total = 0 while (count < y): total += count count += 1 return (total) print(whilefunc(5)) def forfunc(y): total = 0 for x in range(y): total += x return(total) print(forfunc(5))

  11. More on Range: def f(): for x in range(-3,3): # from -3 up to but not including 3, e.g., [-3,-2,-1,0,1,2] print(x) return(x) print(f()) def f(): for x in range(-3,3,2): # from -3 up to but not including 3, by increments of 2, e.g., [-3,-1,1] print(x) return(x) print(f()) (Can we make a loop go backwards?)

  12. What does this do? def f(): y = 1000 total = 0 for x in ["2","7","1","9"]: total = total + int(x) * int(y) y /=10 return(total) print(f())

  13. How about this? def f(z): y = int(input("enter a number: ")) for x in range(1,y): for q in range(1,z): print (str(q)+ "*"+str(x)+"=\t"+str(x*q)) print ("\n") return f(4)

  14. Just about anything you can do with lists: Len, in def f(x): if "e" in x: return("e is in your message.") else: return("e is not in your message.") strvar = “puppies are cute” print(f(strvar)) z = len("cat")

  15. Positions def f(wv): return(wv[3]) wordvar = “cats” print(f(wordvar))

  16. Slicing (Different from Indexing) Copying parts of strings: 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 def f():word = “pizza” return(word[0:5]) def g(): word = “pizza” return(word[1:3]) def h(): word = “pizza” return(word[-4:-2]) def i(): word = “pizza” return(word[-4:3])

  17. Shortcuts 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 word=“pizza” word[0:4] pizz word[:4] pizz word[2:5] zza word[2:] zza

  18. # display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+str(wd[s:f])) print(g(3,7,"sesquipedalian"))

  19. Strings are Immutableword of the day • Can: • x = "catamaran" • print(x.count("a")) • print(x.index('a')) • Can’t (if it changes the string, we can’t do it) • Append (use + instead) • Reverse() • Pop() • Insert() • Sort()

  20. What does this do? def f(): strvar = input("enter a string: ") var1 = "" for x in range(len(strvar) - 1,-1,-1): var1 += strvar[x]; return(var1) print(f())

  21. This one? def f(): strvar = input("enter a number: ") y = 1 z = 0 var1 = "" for x in range(len(strvar) - 1,-1,-1): z += int(strvar[x]) * y; y*=10 return(z) print(f())

  22. What does this give you? def f(lv): x = len(lv) print(x) for y in range(0,x): if "t" in lv[y]: print(lv[y]) return listvar = ["ham","am","boat","goat","there","anywhere"] f(listvar)

  23. What does this do? deff(word): high = len(word) low = 0 newstr= "" for i in range(10): position = randrange(low, high) newstr+= word[position] return(newstr) wvar = "sesquipedalian" print(f(wvar))

  24. How about this one? deff(m): new_m= "" SPECIAL = "dlmstp" for k in m: if k.lower() not in SPECIAL: new_m+= k return(new_m) mvar = "Hi, my name is Lassie" print("Your message is: now" + f(mvar))

  25. Something you can’t do word = “ night”; word[0] = “s”; Instead: newword = “” for x in “night”: if x == “n": newword+= “s" else: newword+= x

  26. What do you get? deff(message): newmessage= "" for x in message: if x == "g": newmessage+= "l" else: newmessage+= x return(newmessage) mvar= "pogysyggabicaggy" print("new string: " + f(mvar))

More Related