Patresss

lab02

Mar 24th, 2015
686
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/user/bin/env python 3.1
  2.  
  3. from sys import argv
  4. from sys import exit
  5. import random
  6. import math
  7.  
  8. import os
  9. os.system('clear')
  10.  
  11. def czy_pierwsza(liczba):
  12.  
  13.  
  14.     l=int(liczba)
  15.     if l < 2:
  16.         return False
  17.     else:
  18.         return all(l%i for i in range(2, int(math.sqrt(l) +1)) )
  19.  
  20. #1__________________________________________
  21. def zad_1():
  22.     print("Zadanie 1: ")
  23.     if len(argv)!=2:
  24.         print("Podaj jeden parametr")
  25.         exit()
  26.  
  27.     licz=argv[1]
  28.     if not licz.isdigit():
  29.         print("Podaj liczbe")
  30.         exit
  31.  
  32.    
  33.     print("Czy liczba", licz,"jest liczba pierwsza:", czy_pierwsza(licz))
  34. #2__________________________________________
  35. def zad_2():
  36.     print("Zadanie 2: ")
  37.     slownik={}
  38.  
  39.     while len(slownik)<50:
  40.         temp=random.randrange(0,100)
  41.         if temp not in slownik:
  42.             slownik[temp]=czy_pierwsza(temp)
  43.        
  44.     print(len(slownik))
  45.     print(slownik)
  46. #3_________________________________
  47. def zad_3():
  48.     print("Zadanie 3: ")
  49.     list_rand=[random.randrange(20) for i in range(100)]
  50.     #print("Lista rand:",list_rand)
  51.  
  52.     slow_n={}
  53.     slow_p={}
  54.  
  55.     for i in range(20):
  56.         if i%2==0:
  57.             slow_p[i]=[j for j, x in enumerate(list_rand) if x == i]
  58.         else:
  59.             slow_n[i]=[j for j, x in enumerate(list_rand) if x == i]
  60.        
  61.     print("Parzyste: ",slow_p)
  62.     #print("Nieparzyste: ",slow_n)
  63.  
  64.    
  65.     slow_new={}
  66.     for key in slow_p:
  67.         if all(i%3 for i in slow_p[key]):
  68.             slow_new[key]=(max(slow_p[key]),min(slow_p[key]))
  69.         else:
  70.             slow_new[key]=[i for i in slow_p[key] if not i%3]
  71.    
  72.     print("Nowa lista:", slow_new)
  73.        
  74. #4__________________________________________
  75. def zad_4():
  76.     print("Zadanie 4: ")
  77.  
  78.  
  79.  
  80.     if len(argv)!=2:
  81.         print("Podaj jeden parametr")
  82.         exit()
  83.  
  84.     if not argv[1].isdigit():
  85.         print("Podaj liczbe")
  86.         exit
  87.  
  88.     size=int(argv[1])
  89.     slownik={i:random.randrange(2,15) for i in range(size)}
  90.     print("Slownik:",slownik)
  91.  
  92.     slownik_odw={w:k for k,w in slownik.items()}
  93.     print("Slownik odwrocony:",slownik_odw)
  94.     lista=[(k,w) for k,w in slownik_odw.items()]
  95.     lista.append(slownik_odw)
  96.  
  97.     print("Lista", lista)
  98. #5_________________________________
  99. def zad_5():
  100.     print("Zadanie 5: ")
  101.     list_rand=[random.randrange(0, 11) for i in range(100)]
  102.     print("Lista rand:",list_rand)
  103.  
  104.     slow={}
  105.  
  106.     for i in range(11):
  107.         slow[i]=[j for j, x in enumerate(list_rand) if x == i]
  108.  
  109.     print("Slownik:", slow)
  110.  
  111. #6_________________________________
  112. def zad_6():
  113.     print("Zadanie 6: ")
  114.     size=10
  115.     slow1={i:random.randrange(1,100) for i in range(size)}
  116.     slow2={i:random.randrange(1,100) for i in range(size)}
  117.  
  118.     slow1_od={w:k for k,w in slow1.items()}
  119.     slow2_od={w:k for k,w in slow2.items()}
  120.  
  121.     print("Slownik 1:",slow1)
  122.     print("Slownik 2:",slow2)
  123.  
  124.     print("Slownik 1 odwrocony:",slow1_od)
  125.     print("Slownik 2 odwrocony:",slow2_od)
  126.    
  127.  
  128.     #for i in slow1_od:
  129.     #   if i in slow2_od:
  130.     #       slow_n[i]=(slow1_od[i],slow2_od[i])
  131.  
  132.    
  133.     slow_n={k:(slow1_od[k],slow2_od[k]) for k in slow1_od if k in slow2_od}
  134.  
  135.  
  136.     print(slow_n)
  137.  
  138.  
  139. #main________________________________
  140.  
  141. #zad_1()
  142. #zad_2()
  143. #zad_3()
  144. #zad_4()
  145. #zad_5()
  146. zad_6()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×