Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. -------------------------------
  2. re z lekcji:
  3.  
  4.  
  5.  
  6. import re
  7.  
  8. l = "avf76bau789bu"
  9.  
  10.  
  11. num = re.findall(r'\d+', l)
  12. print(num)
  13.  
  14. num = re.findall('a', l)
  15. print(len(num))
  16.  
  17. num = re.findall(r'\D+', l)
  18. for i in num:
  19. if len(i)<4:
  20. print(i, end=' ')
  21.  
  22. n = "dfdf.glh fdfdf.ty gkhkh.py mfmfm.mp asa.py"
  23. print("\n")
  24. num = n.split(" ")
  25. for i in num:
  26. if i.endswith(".py") == True:
  27. print(i, end=' ')
  28.  
  29.  
  30.  
  31. -----------------------------------
  32. moje kody z lekcji:
  33.  
  34.  
  35.  
  36. s = "qwertyuiopasdfghjkl"
  37. l = len(s)
  38. for i in range(0,l):
  39. print (s[l-i-1], end = "")
  40.  
  41.  
  42.  
  43. lista = ['abcd', 786, 2.23, 'john', 70.2, 'abcd', 786, 2.23, 'john', 70.2, 'abcd', 786, 2.23, 'john', 70.2, 'abcd', 786, 2.23, 'john', 70.2]
  44. print (type(lista[i]))
  45. for i in range (0,19):
  46. if (type(lista[i]) is str):
  47. print (lista[i])
  48.  
  49.  
  50. lista2 = ['asah', 'ashhga', 'ahsa', 'ashia', 'ghasa', 'akusa', 'asya', 'aassa', 'dfvasa', 'adfvsa', 'asvfa', 'asadsf', 'adfsa', 'asda', 'assdga', 'afsa', 'asssa', 'afsa', 'dasa', 'assa']
  51. lista2.sort()
  52. print (lista2)
  53.  
  54.  
  55. def pierwszaFun ( str ):
  56. "Oto pierwsza funkcja"
  57. print ("Czesc, jestem pierwsza funkcja")
  58. print (str)
  59. return
  60. #wywolanie funkcji
  61. pierwszaFun("Czesc, milo mi")
  62. #wyswietlenie opisu funkcji
  63. print (pierwszaFun.__doc__)
  64.  
  65.  
  66.  
  67. def pisanie(naz1, naz2, naz3):
  68. fo = open(naz1, 'r')
  69. x = fo.read()
  70. fo_2 = open(naz2, 'w+')
  71. fo_3 = open(naz3, 'w+')
  72. print(x)
  73. tab = x.split(" ")
  74. for i in tab:
  75. if ((int)(i) % 2) == 0:
  76. fo_2.write(i)
  77. fo_2.write(" ")
  78. else:
  79. fo_3.write(i)
  80. fo_3.write(" ")
  81. fo.close()
  82. fo_2.close()
  83. fo_3.close()
  84.  
  85. pisanie("Liczby2.txt", "jeden.txt", "dwa.txt")
  86.  
  87.  
  88.  
  89.  
  90.  
  91. def argum3(*num):
  92. "jeden arg wymagany, wiele dowolnych"
  93. sum = 0
  94. print ("Podales argumenty:")
  95. for i in num:
  96. print (i)
  97. sum = sum + i
  98. print ("Suma:",sum)
  99. return
  100.  
  101. argum3(10, 5, 4, 6, 9)
  102.  
  103.  
  104. n = (int)(input("Wpisz n: "))
  105.  
  106. fo = open('Liczby.txt', 'w+')
  107.  
  108. for i in range( 0, n ):
  109. l = input()
  110. fo.write(l)
  111. fo.write(" ")
  112.  
  113. #print ("Pisze do pliku...")
  114. #print ("--pozycja w pliku:",fo.tell())
  115. #fo.seek(0)
  116. #print ("--pozycja w pliku:",fo.tell())
  117. #str1 = fo.read()
  118. #print ("Przeczytalem z pliku:\n",str1)
  119. #print ("--pozycja w pliku:",fo.tell())
  120. #fo.seek(10)
  121. #print ("--pozycja w pliku:",fo.tell())
  122. #3str2 = fo.read(10)
  123. #print ("Przeczytalem z pliku 10 znakow:\n",str2)
  124. fo.close()
  125.  
  126. '''
  127.  
  128. '''
  129.  
  130. class MyFirstClass: #deklaracja klasy
  131. 'To jest pierwsza klasa w Pythonie' #doc klasy
  132. def sayHello(self): #deklaracja metody
  133. print("Hello Pythoniku!!") #cialo metody
  134. mfc = MyFirstClass() #tworzenie instancji
  135. mfc.sayHello() #wywolanie metody
  136.  
  137.  
  138. class A:
  139. def __init__(self):
  140. self.x = " x - klasa A"
  141. self.z = " z - klasa A"
  142. def __str__(self):
  143. return "Jestem klasa A i mam sobie: " + self.x + self.z
  144. class C(A):
  145. def __init__(self):
  146. A.__init__(self)
  147. self.x = " x - klasa C"
  148. self.y = " y - klasa C"
  149. def __str__(self):
  150. s += "Jestem klasa C i mam sobie: " + self.x + self.y
  151. return s
  152. a = A()
  153. c = C()
  154. # kopia obiektu c i rzutowanie na klase A
  155. import copy
  156. b = copy.copy(c)
  157. b.__class__ = A
  158. for o in (a,b,c):
  159. print("*******")
  160. print(o)
  161.  
  162. '''
  163.  
  164. '''
  165.  
  166.  
  167. class Super:
  168. 'Jest to klasa nadrzędna'
  169. def method(self):
  170. print("jestem w Super.method")
  171. def delegate(self):
  172. self.action() #metoda oczekuje zdefiniowania
  173. class Inheritor(Super):
  174. 'dziedzic - dziedziczy wszystko po klasie Super i nic nie dodaje'
  175. pass
  176. class Replacer(Super):
  177. 'Calkowicie zamienia metode method'
  178. def method(self):
  179. print("jestem w Replacer.method")
  180. class Extender(Super):
  181. 'Rozszerza działanie metody method'
  182. def method(self):
  183. print("poczatek Extender.method")
  184. Super.method(self)
  185. print("koniec Extender.method")
  186. class Provider(Super):
  187. 'Dostarcza wymaganą implementację metody action'
  188. def action(self):
  189. print("Akcja!!")
  190. if __name__ == '__main__':
  191. for klass in (Super,Inheritor,Replacer,Extender,Provider):
  192. print('\n'+klass.__name__+" ")
  193. klass().method()
  194. if(klass.__name__ == "Provider"):
  195. klass().delegate()
  196.  
  197. '''
  198.  
  199. class Pair:
  200. def __init__(self, a, b):
  201. self.a = a
  202. self.b = b
  203. def operation(self):
  204. self.operation()
  205. class Greater(Pair):
  206. def __init__(self, a, b):
  207. Pair.__init__(self, a, b)
  208. def operation(self):
  209. print(max(self.a,self.b))
  210. g = Greater(3,5)
  211. g.operation()
  212.  
  213.  
  214. # class PozaZakresem(Exception):
  215. # def __str__(self):
  216. # return"Poza zakresem"
  217. # class Pomiar:
  218. # def __init__(self, dana):
  219. # self.dana = dana
  220. # if self.dana<-20 or self.dana>20:
  221. # raise PozaZakresem
  222.  
  223. #p = Pomiar(20)
  224.  
  225. class PozaZakresem(Exception):
  226. def __str__(self):
  227. return"Poza zakresem"
  228. class Pomiar:
  229. def __init__(self, dana):
  230. self.dana = dana
  231. if self.dana<-20 or self.dana>20:
  232. raise PozaZakresem
  233.  
  234.  
  235. fo = open('Pomiary.txt', 'w+')
  236. n = int(input("Podaj n:"))
  237. for i in range (0, n):
  238. l = int(input())
  239. try:
  240. p = Pomiar(l)
  241. fo.write(str(l))
  242. fo.write(" ")
  243. except:
  244. PozaZakresem
  245.  
  246.  
  247.  
  248.  
  249.  
  250. ---------------------------
  251. rozwiązania Tomka:
  252.  
  253. napis = input("Podaj napis: ")
  254.  
  255. odkad = int(len(napis)/2)
  256.  
  257. for i in range(odkad, len(napis)):
  258. print(napis[i], end='')
  259.  
  260.  
  261. 2 nie dziala
  262. plik = open("Calk.txt", "r+")
  263. line = plik.read()
  264. liczby = line.split()
  265.  
  266. print(liczby)
  267.  
  268. maksymalna = max(liczby)
  269.  
  270. print("Max: ", maksymalna)
  271.  
  272. 3
  273. class End:
  274. def __init__(self, napis, n):
  275. self.napis = napis
  276. self.n = n
  277.  
  278. def last(self):
  279. for i in range(len(self.napis)-self.n, len(self.napis)):
  280. print(self.napis[i], end='')
  281.  
  282. sprawdz = End("jakis", 3)
  283. sprawdz.last()
  284.  
  285.  
  286. 4
  287. class Mult5(Exception):
  288. def __str__(self):
  289. return "Liczba jest podzielna przez 5"
  290.  
  291. class Data:
  292. def __init__(self, x):
  293. if x % 5 == 0:
  294. raise Mult5()
  295.  
  296. try:
  297. sprobuj = Data(6)
  298. print("Nie jest podzielna przez 5")
  299.  
  300. except Mult5 as m5:
  301. print(m5)
  302.  
  303.  
  304.  
  305. -------------------------------
  306. rozwiązania Krystka:
  307.  
  308.  
  309.  
  310. napis = input("Podaj ciag znakow: ")
  311.  
  312. if len(napis) % 2 == 0:
  313. polowa = len(napis) // 2
  314. else:
  315. polowa = (len(napis) // 2) + 1
  316.  
  317. for i in range(0, polowa):
  318. print(napis[i], end='')
  319.  
  320.  
  321.  
  322. 2
  323. plik = open("Dane.txt", "r+")
  324. liczby = plik.read()
  325. listaLiczb = liczby.split(' ')
  326.  
  327. suma = 0
  328. for i in listaLiczb:
  329. suma += int(i)
  330.  
  331. print(suma)
  332.  
  333.  
  334.  
  335. 3
  336. class Short:
  337. lancuch = ''
  338. liczba = 0
  339.  
  340. def __init__(self, lancuch, liczba):
  341. self.lancuch = lancuch
  342. self.liczba = liczba
  343.  
  344. def first(self):
  345. for i in range(0, self.liczba):
  346. print(self.lancuch[i], end='')
  347.  
  348.  
  349. prefiks = Short('Dziendobry', 5)
  350. prefiks.first()
  351.  
  352.  
  353.  
  354. 4
  355. class Odd(Exception):
  356. def __str__(self):
  357. return "Liczba jest nieparzysta!"
  358.  
  359.  
  360. class Number:
  361. def __init__(self, liczba):
  362. if liczba % 2 != 0:
  363. raise Odd()
  364.  
  365.  
  366. try:
  367. Number(10)
  368. print("Success")
  369. Number(11)
  370. print("Success")
  371. except Odd as od:
  372. print(od)
  373.  
  374.  
  375. 5
  376. import re
  377.  
  378. linia = input("Podaj linie tekstu: ")
  379.  
  380. podciagi = re.findall(r"d.*r", linia)
  381.  
  382. print(max(podciagi))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement