Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import sqlite3
  2. import sys
  3. import re
  4. import zipfile
  5. from urllib import request
  6. import os
  7. from hashlib import md5
  8. from stat import *
  9. from datetime import datetime
  10.  
  11.  
  12. def problema1(s):
  13. cuvinte = re.findall('[a-zA-Z0-9_]+', s)
  14. return sorted(cuvinte)
  15.  
  16.  
  17. def problema2(s, url):
  18. try:
  19. page = request.urlopen(url)
  20. return str.encode(s) in page.read()
  21. except:
  22. return False
  23.  
  24.  
  25. def problema3(path):
  26. lista = []
  27. for file in os.listdir(path):
  28. lista.append(md5(file.encode()).hexdigest())
  29. return sorted(lista)
  30.  
  31.  
  32. def problema4():
  33. lista = set()
  34. path = ""
  35. for i in range(1, len(sys.argv)):
  36. path += sys.argv[i]
  37. if i != len(sys.argv) - 1:
  38. path += " "
  39. for file in os.listdir(path):
  40. new_path = os.path.join(path, file)
  41. mode = os.stat(new_path).st_mode
  42. if S_ISDIR(mode) == 0:
  43. lista.add(os.stat(os.path.join(path, file)).st_size)
  44. return sorted(lista)
  45.  
  46.  
  47. def problema5(cod):
  48. lista = cod.split("\n")
  49. x = 0
  50. for instr in lista:
  51. op = instr.split(" ")[1]
  52. nr = int(instr.split(" ")[2])
  53. if op == "egal":
  54. x = nr
  55. if op == "plus":
  56. x += nr
  57. if op == "minus":
  58. x -= nr
  59. if op == "impartit":
  60. x //= nr
  61. if op == "inmultit":
  62. x *= nr
  63. return x
  64.  
  65.  
  66. def problema7():
  67. lista = []
  68. arguments = sys.argv
  69. for i in range(1, len(arguments)):
  70. date = arguments[i]
  71. dateTime = datetime(int(date[6]) * 1000 + int(date[7]) * 100 + int(date[8]) * 10 + int(date[9]),
  72. int(date[0]) * 10 + int(date[1]),
  73. int(date[3]) * 10 + int(date[4]),
  74. int(date[11]) * 10 + int(date[12]),
  75. int(date[14]) * 10 + int(date[15]),
  76. int(date[17]) * 10 + int(date[18]))
  77. lista.append(dateTime)
  78. lista.sort(reverse=True)
  79. maxim = 0
  80. for i in range(len(lista)):
  81. for j in range(i + 1, len(lista)):
  82. dif = (lista[i] - lista[j]).total_seconds()
  83. maxim = max(maxim, dif)
  84. str_lista = []
  85. for date in lista:
  86. str_lista.append(
  87. str(date.year) + "-" +
  88. ("0" if date.month < 10 else "") + str(date.month) + "-" +
  89. ("0" if date.day < 10 else "") + str(date.day) + " " +
  90. ("0" if date.hour < 10 else "") + str(date.hour) + ":" + \
  91. ("0" if date.minute < 10 else "") + str(date.minute) + ":" +
  92. ("0" if date.second < 10 else "") + str(date.second))
  93. return str_lista, int(maxim)
  94.  
  95.  
  96. def problema8(path="", low=0, high=0):
  97. ok = 0
  98. for root, directories, files in os.walk(path):
  99. for filename in files:
  100. if filename.endswith('.zip'):
  101. zip = zipfile.ZipFile(root + os.sep + filename)
  102. for zipinfo in zip.infolist():
  103. if not zipinfo.is_dir():
  104. if zipinfo.orig_filename.endswith('sample.sqlite'):
  105. zip.extract(zipinfo)
  106. fileName = zipinfo.orig_filename
  107. sqlite = sqlite3.connect(fileName)
  108. curs = sqlite.cursor()
  109. curs2 = sqlite.cursor()
  110. curs3 = sqlite.cursor()
  111. curs.execute(
  112. 'select * from tracks where Milliseconds<={} and Milliseconds>={}'.format(high, low))
  113. l = list()
  114. for row in curs:
  115. curs2.execute('select Name from genres where GenreId={}'.format(row[4]))
  116. curs3.execute('select Title from albums where AlbumId={}'.format(row[2]))
  117. nume_gen = curs2.fetchone()[0]
  118. nume_album = curs3.fetchone()[0]
  119. nume_piesa = row[1]
  120. l.append((nume_album, nume_piesa, nume_gen))
  121. return sorted(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement