Advertisement
patas99

Untitled

Jan 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.24 KB | None | 0 0
  1. #########################################prvocisla##############################
  2. #!/usr/bin/python
  3.  
  4. def is_prime(n):
  5.     for i in range(3, n):
  6.         if n % i == 0:
  7.             return False
  8.     return True
  9.  
  10. def salam(pole):
  11.     new = []
  12.     for i in range (0,len(pole)):
  13.         text = str(pole[i])
  14.         pocet = 0
  15.         for j in range(6):
  16.             pomocna = 0
  17.             for k in range(6):
  18.                 if text[j] == text[k]:
  19.                     pomocna += 1
  20.                 if pomocna >=3:
  21.                     pocet += 1
  22.                     znak = text[j]
  23.                     break
  24.         if pocet >= 1:
  25.             new.append(pole[i])
  26.             neww = []
  27.             for o in range(1, 10):
  28.                 cislo = int(text.replace(znak, str(o)))
  29.                 if is_prime(cislo):
  30.                     neww.append(cislo)
  31.                 if len(neww) == 8:
  32.                     return neww
  33.     print(new)
  34.  
  35. def uloz_do_souboru(pole):
  36.     file = open("cisla.txt","w")
  37.     for item in pole:
  38.         file.write("%s\n" % item)
  39.  
  40.  
  41. if __name__ == '__main__':
  42.     N = 1000000
  43.     polee = [1] * N
  44.  
  45.     for i in range(2, N):
  46.         if polee[i]:
  47.             j = 2
  48.             k = i * j
  49.             while k < N:
  50.                 polee[k] = 0
  51.                 j = j + 1
  52.                 k = i * j
  53.     pole = list()
  54.     for i in range(100000, N):
  55.         if polee[i]:
  56.             pole.append(i)
  57.  
  58.     print(pole)
  59.  
  60.     pole1 = salam(pole)
  61.     print(pole1)
  62.     uloz_do_souboru(pole1)
  63. ############################json z netu#################################
  64. #!/usr/bin/python
  65. import json
  66. import urllib.request
  67.  
  68. def download_data(url,filename):
  69.     testfile = urllib.request.URLopener()
  70.     testfile.retrieve(url,filename)
  71.  
  72. def load_file(file):
  73.     text = ""
  74.     with open(file,"r") as fp:
  75.         text = fp.read()
  76.     return text
  77.  
  78. def save_to_file(text,file):
  79.     with open(file,"w") as fp:
  80.         fp.writelines(text)
  81.  
  82. def get_json_data(file):
  83.     print(json)
  84.     with open(file) as data_file:
  85.         data = json.load(data_file)
  86.     return data
  87.  
  88. def save_json_to_file(file, json_data):
  89.     with open(file, "w") as outfile:
  90.         json.dump(json_data, outfile)
  91.  
  92. def parse_titles(json_data):
  93.     list_dicts = list()
  94.     for line in json_data:
  95.         section = line["section"]
  96.         title = line["title"]
  97.         result_data = dict()
  98.         result_data[section] = title
  99.         list_dicts.append(result_data)
  100.     return list_dicts
  101.  
  102. def parse_urls(json_data):
  103.     result_urls = list()
  104.     for line in json_data:
  105.         media = line["media"]
  106.         for m in media:
  107.             metadata = m["media-metadata"]
  108.             for meta in metadata:
  109.                 media_format = meta["format"]
  110.                 if media_format == "Standard Thumbnail":
  111.                     result_urls.append(meta["url"])
  112.     return result_urls
  113.  
  114. def parse_article_keyword(json_data):
  115.     article_keywords = dict()
  116.     for line in json_data:
  117.         title = line["title"]
  118.         article_keywords[title] = list()
  119.         keywords_string = line["adx_keywords"]
  120.         keywords = keywords_string.split(";")
  121.         for word in keywords:
  122.             article_keywords[title].append(word)
  123.     return article_keywords
  124.  
  125. def parse_keyword_occurence(json_data):
  126.     all_keyword = dict()
  127.     many_occures = list()
  128.     for line in json_data:
  129.         keywords_string = line["adx_keywords"]
  130.         keywords = keywords_string.split(";")
  131.         for word in keywords:
  132.             if all_keyword.get(word) is None:
  133.                 all_keyword[word] = 1
  134.             else:
  135.                 all_keyword[word] +=1
  136.     for word,count in all_keyword.items():
  137.         if count >= 3:
  138.             many_occures.append(word)
  139.     return  many_occures
  140.  
  141. def get_keyword_occures_count(article_keywords, word_to_search):
  142.     clanky = list()
  143.     for title, article_keywords_list in article_keywords.items():
  144.         count = 0
  145.         for word in word_to_search:
  146.             if word in article_keywords_list:
  147.                 count +=1
  148.         if count >1:
  149.             clanky.append(title)
  150.     return clanky
  151. if __name__ == '__main__':
  152.     download_data("http://www.nti.tul.cz/~vrany/pjp_data/popular-articles.json", "popular-articles.json.txt")
  153.     data = get_json_data("popular-articles.json.txt")
  154.     print (data)
  155.     titles_data = parse_titles(data)
  156.     print("Delka listu z bodu 1 je {0}".format(len(titles_data)))
  157.     print("Treti polozka titles je {0}".format(titles_data[2]))
  158.     save_json_to_file("titles.json", titles_data)
  159.  
  160.     url_data = parse_urls(data)
  161.     print("Dvacata polozka urls je {0}".format(url_data[20]))
  162.     print("Delka listu z bodu 2 je {0}".format(len(url_data)))
  163.     save_json_to_file("urls.json", url_data)
  164.  
  165.     keyword_data = parse_keyword_occurence(data)
  166.     print("Klicovych slov s vyskytem >= 3 je {0}, jde o slova:".format(len(keyword_data)))
  167.     print(keyword_data)
  168.  
  169.     article_keyword_data = parse_article_keyword(data)
  170.     print(len(article_keyword_data))
  171.  
  172.     clanky = get_keyword_occures_count(article_keyword_data, keyword_data)
  173.     print("Clanky s vyskytem techto slov vice nebo rovnou 3:")
  174.     print(clanky)
  175.  
  176.     save_to_file(clanky, "answer.txt")
  177. ###############################textak na disku###############################
  178. import sys
  179. import re
  180. def nacti():
  181.     if len(sys.argv)>1:
  182.         try:
  183.             soubor = open(sys.argv[1], 'r')
  184.         except IOError:
  185.             print('\n    !!!Nepodarilo se otevrit soubor!!!')
  186.             print(helpme())
  187.             quit()
  188.     else:
  189.         print(helpme())
  190.         quit()
  191.     salam = list()
  192.     slova = list()
  193.  
  194.     for line in soubor:
  195.         salam.append(line)
  196.         slova.append(line.strip('\n\t'))
  197.     if "-c" not in sys.argv and "-w" not in sys.argv and "-l" not in sys.argv and "-L" not in sys.argv and "-f" not in sys.argv:
  198.         print(get_pocet_radku(slova),get_pocet_slov(slova),get_pocet_znaku(salam))
  199.     if "-c" in sys.argv:
  200.         print(get_pocet_znaku(salam))
  201.     if "-w" in sys.argv:
  202.         print(get_pocet_slov(slova))
  203.     if "-l" in sys.argv:
  204.         print(get_pocet_radku(slova))
  205.     if "-L" in sys.argv:
  206.         print(get_delku_radku(slova))
  207.     if "-f" in sys.argv:
  208.         print(vypis_slovnik(frekvence_znaku(slova)))
  209.  
  210. def vypis_slovnik(slovnik):
  211.     '''vypise slovnik ve formatu klic hodnota'''
  212.     for line in slovnik:
  213.         print (line + ' ' + str(slovnik[line]))
  214.  
  215. def frekvence_znaku(slova):
  216.     '''vrati frekvenci jednotlivych znaku'''
  217.     frekvence = {}
  218.     for line in slova:
  219.         for char in line:
  220.             if char in frekvence:
  221.                 frekvence[char] += 1
  222.             else:
  223.                 frekvence[char] = 1
  224.     return frekvence
  225.  
  226. def get_delku_radku(slova):
  227.     delka = 0
  228.     for line in slova:
  229.         if len(line) > delka:
  230.             delka = len(line)
  231.     return delka
  232.  
  233. def get_pocet_znaku(slova):
  234.     delka = 0
  235.     for line in slova:
  236.         for word in line:
  237.             delka +=1
  238.     return delka
  239.  
  240. def get_pocet_radku(slova):
  241.     return len(slova)
  242.  
  243. def get_pocet_slov(slova):
  244.     delka = 0
  245.     for line in slova:
  246.         delka += len(line.split())
  247.     return delka
  248.  
  249. def helpme():
  250.     print("ses salam")
  251.  
  252. if __name__ == '__main__':
  253.     nacti()
  254. ###########################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement