-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
56 lines (40 loc) · 1.03 KB
/
examples.py
File metadata and controls
56 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import random
from urllib.request import urlopen
def fibonacci(limit=100):
"""
Fibonacci interativo usando yield
"""
a, b = 0, 1
while b < limit:
yield b
a, b = b, a + b
return
def get_url(url):
with urlopen(url) as response:
data = response.read().decode("latin1")
return data
def write_file(filename, content):
with open(filename, "w") as f:
f.write(content)
def generate_random_data(limit=100):
numbers = random.sample(range(1, 10001), limit)
return numbers
def main():
print("Fibonacci:")
for i in fibonacci(1000):
print(i)
content = get_url("https://www.google.com.br/")
chars = {}
for i in content:
if i in chars:
chars[i] += 1
else:
chars[i] = 0
print(json.dumps(chars, indent=4))
random_data = generate_random_data()
write_file("random.txt", "\n".join([str(i) for i in random_data]))
if __name__ == '__main__':
main()