Advertisement
Guest User

CSCircles Leosungen

a guest
Dec 13th, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.45 KB | None | 0 0
  1. """
  2. Author: Adrian
  3. Licence: none
  4. """
  5. Programmierübung : Python Adder
  6.  
  7. S = input()
  8. for i in range(0, len(S)-1):
  9.    c = S[i:i+1]
  10.  
  11.    d = i
  12.    if c == "+":
  13.       break
  14. a = int(S[0:i])+int(S[i+1:len(S)])
  15. print(a)
  16.  
  17.  
  18.  
  19. Programmierübung : Substring Counting
  20.  
  21. count = 0
  22. Nadel = str(input())
  23. Heuhaufen = str(input())
  24.  
  25. for i in range(0 , len(Heuhaufen)): #-len(Nadel)):
  26.    Kram = Heuhaufen[i:i+len(Nadel)]
  27.    if Kram == Nadel:
  28.       count = count + 1
  29. print(count)
  30.  
  31.  
  32.  
  33. Programmierübung : Watch the Pendulum
  34.  
  35. import math
  36. # import statement goes here
  37. L = float(input())
  38. A = float(input())
  39. # Lösche diesen Kommentar und gib deinen Code hier ein
  40. for i in range(0,10):
  41.    x = L * math.cos(A * math.cos(i * math.sqrt(9.8/L))) - L * math.cos(A)
  42.    print(x)
  43.  
  44.  
  45.  
  46. Programmierübung : Centering Text
  47.  
  48. x = "Hallo"
  49.  
  50. width = int(input())
  51.  
  52. while True:
  53.    line = input()
  54.    add = 0
  55.    if line == "END":
  56.       break
  57.    if len (line) > width:
  58.       print("Du schreibst zu viel")
  59.       continue
  60.    if len(line)+width%2 != 0 :
  61.       add = 1
  62.    leftdots = int(((width - len(line)) / 2))*(".")
  63.    rightdots = leftdots
  64.    if len(leftdots)+len(rightdots)+len(line) < width :
  65.       leftdots = leftdots + "."
  66.    
  67.    print(leftdots + line + rightdots)
  68.  
  69.    if line == "END":
  70.       break
  71. # Lösche diesen Kommentar und gib deinen Code hier ein
  72. #print(width)
  73. #print(A)
  74.  
  75.  
  76.  
  77. Programmierübung : Ending Time
  78.  
  79. start = input()
  80. duration = int(input())
  81. hour = int(start[0:2])
  82. minutes = int(start[3:5])
  83.  
  84. newminutes = minutes + duration
  85. while newminutes >= 60 :
  86.    newminutes = newminutes - 60
  87.    hour = hour + 1
  88. while hour >= 24 :
  89.    hour = hour - 24
  90. if len(str(newminutes)) == 1:
  91.    newminutes = "0" + str(newminutes)
  92. if len(str(hour)) == 1:
  93.    hour = "0" + str(hour)
  94. print(str(hour)+":"+str(newminutes))
  95.  
  96.  
  97.  
  98. Programmierübung : Character Map
  99.  
  100. start = 31 #doesn't matter
  101. end = 31
  102. while(end < 127):
  103.    start = end + 1
  104.    end = start + 15
  105.    chrZeile = "chr:"
  106.    ordZeile = "asc:"
  107.    for i in range(start, end+1):
  108.       symbolstr = str(chr(i))
  109.       if len(symbolstr) == 1:
  110.          symbolstr = "  "+symbolstr+" "
  111.       elif len(symbolstr) == 2:
  112.          symbolstr = " "+symbolstr+" "
  113.       elif len(symbolstr) == 3:
  114.          symbolstr = " "+symbolstr
  115.       chrZeile = chrZeile + symbolstr
  116. #
  117.    for i in range(start, end+1):
  118.       ordstr = str(i)
  119.       if len(ordstr) == 1:
  120.          ordstr = "  "+ordstr+" "
  121.       elif len(ordstr) == 2:
  122.          ordstr = " "+ordstr+" "
  123.       elif len(ordstr) == 3:
  124.          ordstr = " "+ordstr
  125.       ordZeile = ordZeile + ordstr
  126.    print(chrZeile)
  127.    print(ordZeile)
  128.  
  129.  
  130.  
  131. Programmierübung : Absoluter Wert
  132.  
  133. x=int(input())
  134. y=x
  135. if x < 0:
  136.    y = (-1)*x
  137. print(y)
  138.  
  139.  
  140.  
  141. Programmierübung : First, Second, Third
  142.  
  143. x = input()
  144. x = str(x)
  145. if x == "1":
  146.    n = x+"st"
  147. elif x == "2":
  148.    n = x+"nd"
  149. elif x == "3":
  150.    n = x+"rd"
  151. else:
  152.    n = x+"th"
  153. print(n)
  154.  
  155.  
  156.  
  157. Programmierübung : 26 Letters
  158.  
  159. letter = input()
  160. x = ord(letter)
  161. if x>=ord("A") and x<=ord("Z"):
  162.     print(x-ord("A")+1)
  163. else:
  164.     print('invalid')
  165.  
  166.  
  167.  
  168.  
  169. Programmierübung : Cubism
  170.  
  171. #x = input()
  172.  
  173. def cube(n):
  174.    return n*n*n
  175. #x = input()
  176. #print(cube(x))
  177.  
  178.  
  179.  
  180. Programmierübung : Rectangle
  181.  
  182. def rectanglePerimeter(width, height):
  183.    return 2*width+2*height
  184.  
  185.  
  186.  
  187. Programmierübung : Lower-Case Characters
  188.  
  189. def lowerChar(char):
  190.    if ord(char) >= 65 and ord(char) <= 90:
  191.       char = chr(ord(char) + 32)
  192.    return char
  193.  
  194.  
  195.  
  196. Programmierübung : Lower-Case Strings
  197.  
  198.  
  199. def lowerChar(char):
  200.    if ord(char) >= 65 and ord(char) <= 90:
  201.       char = chr(ord(char) + 32)
  202.    return char
  203.  
  204. def lowerString(string):
  205.    result = ""
  206.    for i in range(0, len(string)):
  207.       result = result + lowerChar(string[i])
  208.    return result
  209.      
  210.  
  211.  
  212.  
  213.  
  214. Programmierübung : Hypotenuse
  215.  
  216. import math
  217. def hypotenuse(a, b):
  218.    return math.sqrt(a*a+b*b)
  219.  
  220.  
  221.  
  222. Programmierübung : Die Dreiecke sind rechteckig
  223.  
  224. def rightTrianglePerimeter(a, b):
  225.    return a + b + hypotenuse(a, b)
  226.  
  227.  
  228.  
  229. Programmierübung : 2D Distance
  230.  
  231. def distance2D(x1, y1, x2, y2):
  232.    dx = x1-x2
  233.    dy = y1-y2
  234.    return hypotenuse(dx, dy)
  235.  
  236.  
  237.  
  238. Programmierübung : Secure the Perimeter
  239.  
  240. def trianglePerimeter(xA, yA, xB, yB, xC, yC):
  241.    return distance2D(xA, yA, xB, yB) + distance2D(xA, yA, xC, yC) + distance2D(xB, yB, xC, yC)
  242.  
  243.  
  244.  
  245. Programmierübung : Alphabet Aerobics
  246.  
  247. for c in range(0, 26):
  248.  print(chr(ord('A')+c), end = "")
  249.  
  250.  
  251.  
  252.  
  253. Programmierübung : Lucky Sevens
  254.  
  255. for i in range(17, 98, 10):
  256.    print(i)
  257.  
  258.  
  259.  
  260. Sortierübung: One None
  261.  
  262.     «blank line»
  263.     None
  264.     <class 'NoneType'>
  265.     <class 'type'>
  266.  
  267.  
  268.  
  269.  
  270.  
  271. Programmierübung : Monkey in the Middle
  272.  
  273. def middle(L): return L[len(L)//2]
  274.  
  275.  
  276.  
  277. Programmierübung : It's Natural
  278.  
  279. def naturalNumbers(n):
  280.   list = []
  281.   for i in range(1, n+1):
  282.      list += [i]
  283.   return list
  284. #print(naturalNumbers(5))
  285.  
  286.  
  287.  
  288. Programmierübung : Palindrome
  289.  
  290. def isPalindrome(S):
  291.   x = True
  292.   for i in range(len(S)//2):
  293.      if S[i] != S[-i-1]:
  294.         x = False
  295.   return x
  296. #print(isPalindrome(input()))
  297.  
  298.  
  299.  
  300. Programmierübung : Product
  301.  
  302. def prod(L):
  303.   x = 1
  304.   for i in range(len(L)):
  305.      x *= L[i]
  306.   return x
  307.  
  308.  
  309.  
  310. Programmierübung : for in
  311.  
  312. # Lösche diesen Kommentar und gib deinen Code hier ein
  313. def prod(L):
  314.   x = 1
  315.   for i in L:
  316.      x *= i
  317.   return x
  318.  
  319.  
  320.  
  321. Sortierübung: à la Mode
  322.  
  323.  
  324.          return i
  325.      for i in L:
  326.      for i in range(0, 10):
  327.        if frequency[i]==max(frequency):
  328.    def mode(L):
  329.      frequency = [0]*10
  330.        frequency[i] = frequency[i] + 1
  331.  
  332.  
  333.  
  334.  
  335.  
  336. Programmierübung : The Replacements
  337.  
  338. def replace(list, X, Y):
  339.   while X in list:
  340.      x = list.index(X)
  341.      list.remove(X)
  342.      list.insert(x, Y)
  343.   return list
  344.  
  345.  
  346.  
  347. Programmierübung : Exact Postage
  348.  
  349. def postalValidate(S):
  350.   S = S.replace(" ", "")
  351.   #def false():
  352.      #return False
  353.   def check(S):
  354.      x = []
  355.      y = True
  356.  
  357.      for i in range(len(S)):
  358.         if i % 2 == 0:
  359.            #print("JHJHGHJHGHJHGHJHGHJHG")
  360.            x.append(S[i].isalpha())
  361.         else:
  362.            #print("123")
  363.            x.append(S[i].isdigit())
  364.      if False in x or len(S)<1 or len(S)%2 != 0:
  365.         y = False
  366.      return y
  367.        
  368.   if check(S) == True:
  369.      return S.upper()
  370.   else:
  371.      return False
  372. #print(postalValidate("k   2  A  1a"))
  373.  
  374.  
  375.  
  376. Programmierübung : Reading the Program
  377.  
  378. def getBASIC():
  379.   liste = []
  380.   while True:
  381.      x = input()
  382.      liste.append(x)
  383.      if x.endswith("END"):
  384.         break
  385.   return liste
  386.  
  387.  
  388.  
  389. Programmierübung : Geh dort hin
  390.  
  391. def findLine(prog, target):
  392.   for i in range(len(prog)):
  393.      if prog[i].startswith(target):
  394.         return i
  395.  
  396.  
  397.  
  398. Programmierübung : Smart Simulation
  399.  
  400. # here is a broken solution to get you started
  401. def execute(prog):
  402.  location = 0
  403.  x = []
  404.  while True:
  405.    if location==len(prog)-1: return "success"
  406.    T = prog[location].split()[-1]
  407.    location = findLine(prog, T)
  408.    if T in x: return "infinite loop"
  409.    x.append(T)
  410.  
  411.  
  412.  
  413. Programmierübung : BASIC Simulator
  414.  
  415. # def getBASIC from subtask 1
  416. def getBASIC():
  417.   liste = []
  418.   while True:
  419.      x = input()
  420.      liste.append(x)
  421.      if x.endswith("END"):
  422.         break
  423.   return liste
  424. # def findLine from subtask 2
  425. def findLine(prog, target):
  426.   for i in range(len(prog)):
  427.      if prog[i].startswith(target):
  428.         return i
  429. # def execute from subtask 3
  430. def execute(prog):
  431.  location = 0
  432.  x = []
  433.  while True:
  434.    if location==len(prog)-1: return "success"
  435.    T = prog[location].split()[-1]
  436.    location = findLine(prog, T)
  437.    if T in x: return "infinite loop"
  438.    x.append(T)
  439. print(execute(getBASIC()))
  440.  
  441.  
  442. Programmierübung : Forty Below In The Winter
  443.  
  444. x = input()
  445. if "C" in x:
  446.   x = float(x.rstrip("C"))
  447.   f = str(x*9/5+32)
  448.   o = f + "F"
  449. elif "F" in x:
  450.   x = float(x.rstrip("F"))
  451.   c = str((x-32)*5/9)
  452.   o = c + "C"
  453. print(o)
  454.  
  455.  
  456.  
  457. Programmierübung : Credit Check
  458.  
  459. def check(S):
  460.   if len(S) != 19:
  461.      #print("len")
  462.      return False
  463.   for i in range(19):
  464.      if (i+1) % 5 == 0:
  465.         if S[i] != " ":
  466.            #print("nospaces")
  467.            return False
  468.      else:
  469.         #if S[i].isdigit != True:
  470.         if not 48 <= ord(S[i]) <= 57:
  471.            #print("nodigits")
  472.            #print(S[i])
  473.            #print(ord(S[i]))
  474.            return False
  475.   s = S.replace(" ", "")
  476.   q = 0
  477.   for i in range(len(s)):
  478.      q += int(s[i])
  479.   if q % 10 == 0:
  480.      #print("ok")
  481.      return True
  482.   else:
  483.      #print("notdivisibleby10")
  484.      return False
  485.  
  486.  
  487.  
  488. Programmierübung : Poetic Analysis
  489.  
  490. import operator
  491. stuff = True
  492. worddic = {}
  493. while True:
  494.   x = input().lower()
  495.   x = x.split(" ")
  496.   if "###" in x:
  497.      break
  498.   for i in range(len(x)):
  499.      if x[i] in worddic:
  500.         worddic[x[i]] += 1
  501.      else:
  502.         worddic[x[i]] = 0
  503. wordlist = list(worddic.keys())
  504. wordcount = list(worddic.values())
  505. print(wordlist[wordcount.index(max(wordcount))])
  506.  
  507.  
  508.  
  509. Programmierübung : Sei Wählerisch
  510.  
  511. def choose(n,k):
  512.   answer = 1
  513.   for i in range(k):
  514.      answer *= (n-i)/(k-i)
  515.   return int(answer)
  516.  
  517.  
  518.  
  519. Programmierübung : Auto-Decryption
  520.  
  521. #x = str(input()).upper()
  522.  
  523. lg = letterGoodness #[.0817, .0149, .0278, .0425, .1270, .0223, .0202, .0609, .0697, .0015, .0077, .0402, .0241, .0675, .0751, .0193, .0009, .0599, .0633, .0906, .0276, .0098, .0236, .0015, .0197, .0007] #letter goodness
  524.  
  525. def Shifter(x, S):
  526.   f = ""
  527.   x = x.upper()
  528.   for i in range(len(x)):
  529.      no = ord(x[i])+S #NewOrdinal
  530.      if no >= ord("Z"): no -= 26
  531.      if no < ord("A"): no += 26 #kein <= sondern < !!
  532.      if x[i] == " ": no = ord(" ")
  533.      f = f + chr(no)
  534.   return f
  535.  
  536. def Evaluate(Was):
  537.   Liste = []
  538.   #goodness = 0.0
  539.   for i in range(26):
  540.      Wann = Shifter(Was, i) #Was=Input Wann=geschiftet
  541.      goodness = 0.0
  542.      for n in range(len(Was)):
  543.            Buchstabe = Wann[n]
  544.            if Buchstabe != " ":
  545.               goodness += lg[ord(Wann[n])-ord("A")]
  546.      Liste.append(goodness)
  547.   print(Shifter(Was, Liste.index(max(Liste))))
  548.  
  549. eingabe = input()
  550. Evaluate(eingabe)
  551.  
  552.  
  553.  
  554. Programmierübung : Blast Up
  555.  
  556. def countup(n):
  557.  if n == 1:
  558.    print('Blastoff!')
  559.  else:
  560.    countup(n - 1)
  561.  print(n)
  562.  
  563.  
  564.  
  565. Programmierübung : Double Time
  566.  
  567. def countdownBy2(n):
  568.  if n <= 0:
  569.    print('Blastoff!')
  570.  else:
  571.    print(n)
  572.    countdownBy2(n - 2)
  573. # delete this comment and enter your code here
  574.  
  575.  
  576.  
  577.  
  578. Programmierübung : Quersumme
  579.  
  580. def digitalSum(n):
  581.   if n < 10:
  582.      return n
  583.   else:
  584.      return n%10 + digitalSum(n//10)
  585.  
  586.  
  587.  
  588. Programmierübung : Einstellige Quersumme
  589.  
  590. def digitalRoot(n):
  591.   if n <10:
  592.      return n
  593.   else:
  594.      return digitalRoot(digitalSum(n))
  595.  
  596.  
  597.  
  598. Programmierübung : Collatz-Folge
  599.  
  600. def hailstone(n):
  601.   print(int(n))
  602.   if n > 1:
  603.      if n % 2 == 0:
  604.         hailstone(n/2)
  605.      else:
  606.         hailstone(n*3+1)
  607.  
  608.  
  609.  
  610. Programmierübung : Searching a Nested List
  611.  
  612. def nestedListContains(NL, target):
  613.   if isinstance(NL, int):
  614.      if NL == target:
  615.         return True
  616.      else:
  617.         return False
  618.   else:
  619.      contains = False
  620.      for i in range(len(NL)):
  621.         if nestedListContains(NL[i], target) == True:
  622.            contains = True
  623.      return contains
  624.  
  625.  
  626.  
  627.  
  628. Sortierübung: Fractal Ruler
  629.  
  630.       ruler(n - 1)
  631.    def ruler(n):
  632.       ruler(n - 1)
  633.       print(n * '-')
  634.       print('-')
  635.      if n == 1:
  636.      else:
  637.  
  638.  
  639.  
  640.  
  641. Sortierübung: Nibofacci
  642.  
  643.    def Fibonacci(n):
  644.            return Fibonacci(n-1) + Fibonacci(n-2)
  645.        else:
  646.        if (n==1 or n==2):
  647.            return 1
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654. Sortierübung: Fast Fibonacci
  655.  
  656.  
  657.        for i in range(3, n+1):      
  658.    def Fibonacci(n):
  659.            sequence.append(sequence[i-1] + sequence[i-2])
  660.        sequence = [0, 1, 1]  # Fibonacci(0) is 0, Fibonacci(1) and Fibonacci(2) are 1
  661.        return sequence[n]
  662.  
  663.  
  664.  
  665. Programmierübung : Primed for Takeoff
  666.  
  667. #Hab den Tipp mal gelesen, ist ne coole Herangehensweise, vielleicht klappt das ja, ohne den Wikipedia Artikel
  668. N = 1000001
  669. isPrime = []
  670. highestPrime = 2
  671. for i in range(N): isPrime.append(True)
  672. isPrime[0], isPrime[1] = False, False
  673.  
  674. while highestPrime != False:
  675.   for i in range(highestPrime , (((N-1)//highestPrime)+1)):#das mit der 1 rauszufinden, hat ne Weile gedauert
  676.      isPrime[highestPrime*i] = False
  677.   try:
  678.      highestPrime = isPrime.index(True, highestPrime+1)
  679.   except ValueError:
  680.      highestPrime = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement