Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################################prvocisla##############################
- #!/usr/bin/python
- def is_prime(n):
- for i in range(3, n):
- if n % i == 0:
- return False
- return True
- def salam(pole):
- new = []
- for i in range (0,len(pole)):
- text = str(pole[i])
- pocet = 0
- for j in range(6):
- pomocna = 0
- for k in range(6):
- if text[j] == text[k]:
- pomocna += 1
- if pomocna >=3:
- pocet += 1
- znak = text[j]
- break
- if pocet >= 1:
- new.append(pole[i])
- neww = []
- for o in range(1, 10):
- cislo = int(text.replace(znak, str(o)))
- if is_prime(cislo):
- neww.append(cislo)
- if len(neww) == 8:
- return neww
- print(new)
- def uloz_do_souboru(pole):
- file = open("cisla.txt","w")
- for item in pole:
- file.write("%s\n" % item)
- if __name__ == '__main__':
- N = 1000000
- polee = [1] * N
- for i in range(2, N):
- if polee[i]:
- j = 2
- k = i * j
- while k < N:
- polee[k] = 0
- j = j + 1
- k = i * j
- pole = list()
- for i in range(100000, N):
- if polee[i]:
- pole.append(i)
- print(pole)
- pole1 = salam(pole)
- print(pole1)
- uloz_do_souboru(pole1)
- ############################json z netu#################################
- #!/usr/bin/python
- import json
- import urllib.request
- def download_data(url,filename):
- testfile = urllib.request.URLopener()
- testfile.retrieve(url,filename)
- def load_file(file):
- text = ""
- with open(file,"r") as fp:
- text = fp.read()
- return text
- def save_to_file(text,file):
- with open(file,"w") as fp:
- fp.writelines(text)
- def get_json_data(file):
- print(json)
- with open(file) as data_file:
- data = json.load(data_file)
- return data
- def save_json_to_file(file, json_data):
- with open(file, "w") as outfile:
- json.dump(json_data, outfile)
- def parse_titles(json_data):
- list_dicts = list()
- for line in json_data:
- section = line["section"]
- title = line["title"]
- result_data = dict()
- result_data[section] = title
- list_dicts.append(result_data)
- return list_dicts
- def parse_urls(json_data):
- result_urls = list()
- for line in json_data:
- media = line["media"]
- for m in media:
- metadata = m["media-metadata"]
- for meta in metadata:
- media_format = meta["format"]
- if media_format == "Standard Thumbnail":
- result_urls.append(meta["url"])
- return result_urls
- def parse_article_keyword(json_data):
- article_keywords = dict()
- for line in json_data:
- title = line["title"]
- article_keywords[title] = list()
- keywords_string = line["adx_keywords"]
- keywords = keywords_string.split(";")
- for word in keywords:
- article_keywords[title].append(word)
- return article_keywords
- def parse_keyword_occurence(json_data):
- all_keyword = dict()
- many_occures = list()
- for line in json_data:
- keywords_string = line["adx_keywords"]
- keywords = keywords_string.split(";")
- for word in keywords:
- if all_keyword.get(word) is None:
- all_keyword[word] = 1
- else:
- all_keyword[word] +=1
- for word,count in all_keyword.items():
- if count >= 3:
- many_occures.append(word)
- return many_occures
- def get_keyword_occures_count(article_keywords, word_to_search):
- clanky = list()
- for title, article_keywords_list in article_keywords.items():
- count = 0
- for word in word_to_search:
- if word in article_keywords_list:
- count +=1
- if count >1:
- clanky.append(title)
- return clanky
- if __name__ == '__main__':
- download_data("http://www.nti.tul.cz/~vrany/pjp_data/popular-articles.json", "popular-articles.json.txt")
- data = get_json_data("popular-articles.json.txt")
- print (data)
- titles_data = parse_titles(data)
- print("Delka listu z bodu 1 je {0}".format(len(titles_data)))
- print("Treti polozka titles je {0}".format(titles_data[2]))
- save_json_to_file("titles.json", titles_data)
- url_data = parse_urls(data)
- print("Dvacata polozka urls je {0}".format(url_data[20]))
- print("Delka listu z bodu 2 je {0}".format(len(url_data)))
- save_json_to_file("urls.json", url_data)
- keyword_data = parse_keyword_occurence(data)
- print("Klicovych slov s vyskytem >= 3 je {0}, jde o slova:".format(len(keyword_data)))
- print(keyword_data)
- article_keyword_data = parse_article_keyword(data)
- print(len(article_keyword_data))
- clanky = get_keyword_occures_count(article_keyword_data, keyword_data)
- print("Clanky s vyskytem techto slov vice nebo rovnou 3:")
- print(clanky)
- save_to_file(clanky, "answer.txt")
- ###############################textak na disku###############################
- import sys
- import re
- def nacti():
- if len(sys.argv)>1:
- try:
- soubor = open(sys.argv[1], 'r')
- except IOError:
- print('\n !!!Nepodarilo se otevrit soubor!!!')
- print(helpme())
- quit()
- else:
- print(helpme())
- quit()
- salam = list()
- slova = list()
- for line in soubor:
- salam.append(line)
- slova.append(line.strip('\n\t'))
- 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:
- print(get_pocet_radku(slova),get_pocet_slov(slova),get_pocet_znaku(salam))
- if "-c" in sys.argv:
- print(get_pocet_znaku(salam))
- if "-w" in sys.argv:
- print(get_pocet_slov(slova))
- if "-l" in sys.argv:
- print(get_pocet_radku(slova))
- if "-L" in sys.argv:
- print(get_delku_radku(slova))
- if "-f" in sys.argv:
- print(vypis_slovnik(frekvence_znaku(slova)))
- def vypis_slovnik(slovnik):
- '''vypise slovnik ve formatu klic hodnota'''
- for line in slovnik:
- print (line + ' ' + str(slovnik[line]))
- def frekvence_znaku(slova):
- '''vrati frekvenci jednotlivych znaku'''
- frekvence = {}
- for line in slova:
- for char in line:
- if char in frekvence:
- frekvence[char] += 1
- else:
- frekvence[char] = 1
- return frekvence
- def get_delku_radku(slova):
- delka = 0
- for line in slova:
- if len(line) > delka:
- delka = len(line)
- return delka
- def get_pocet_znaku(slova):
- delka = 0
- for line in slova:
- for word in line:
- delka +=1
- return delka
- def get_pocet_radku(slova):
- return len(slova)
- def get_pocet_slov(slova):
- delka = 0
- for line in slova:
- delka += len(line.split())
- return delka
- def helpme():
- print("ses salam")
- if __name__ == '__main__':
- nacti()
- ###########################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement