Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. #Modulo Array
  4. #
  5. #Author: Emilio U. Giuffrida & Students
  6. #Date: 17/10/2018
  7. #
  8. #
  9. #TODO:
  10. #
  11. #1- add comments
  12. #2- add shell sort,insertion sort, quicksort and merge sort
  13.  
  14. from array import *
  15. import os
  16. import random
  17. import sys
  18.  
  19.  
  20. def creaArrayIntStatico(n_Elementi):
  21. newArray=array("i", [])
  22.  
  23. for i in range(0, n_Elementi):
  24. newArray.append(sys.maxint)
  25.  
  26. return newArray
  27.  
  28.  
  29. def creaArrayIntDinamico():
  30. return array("i", [])
  31.  
  32.  
  33. def creaArrayFloatStatico(n_Elementi):
  34. newArray=array("f", [])
  35.  
  36. for i in range(0, n_Elementi):
  37. newArray.append(-1.0)
  38.  
  39. return newArray
  40.  
  41. def creaArrayFloatDinamico():
  42. return array("f", [])
  43.  
  44.  
  45. def stampaArray(myVector):
  46.  
  47. state = isEmpty_v2_0(myVector)
  48.  
  49. if (state == True):
  50. print ("L'array e' vuoto!")
  51.  
  52. return -1
  53. else:
  54. lenght = len (myVector)
  55. for i in range (0, lenght):
  56. print myVector[i],
  57.  
  58. print ""
  59.  
  60. return 0
  61.  
  62.  
  63. def riempiArrayDinamico(n_Elementi, myVector):
  64.  
  65. for i in range(0, n_Elementi):
  66. nuovoElemento=random.randint(1,500)
  67. myVector.append(nuovoElemento)
  68.  
  69. return 0
  70.  
  71.  
  72. def riempiArrayStatico(myVector):
  73. state = isEmpty_v2_0(myVector)
  74.  
  75. if (state == True):
  76. print ("L'array e' vuoto!")
  77.  
  78. return
  79. else:
  80. lenght = len (myVector)
  81. for i in range (0, lenght):
  82. nuovoElemento=random.randint(1,500)
  83. myVector[i]=nuovoElemento
  84. print "Elemento inserito in posizione", i,":",myVector[i]
  85.  
  86. print ""
  87.  
  88. return
  89.  
  90. #Genera un IndexError: array index out of range
  91. def svuotaArray_conErrore(n_Elementi, myVector):
  92.  
  93. for i in range(0, n_Elementi):
  94. elementToRemove=myVector[i]
  95. myVector.remove(elementToRemove)
  96.  
  97. print("Vettore aggiornato", myVector)
  98.  
  99. return
  100.  
  101.  
  102. def svuotaArrayDinamico_con_errore(n_Elementi, myVector):
  103.  
  104. nElementiDecrementata = n_Elementi
  105.  
  106. while(nElementiDecrementata>0):
  107. headElement=myVector[0]
  108. myVector.remove(headElement)
  109. nElementiDecrementata = nElementiDecrementata -1
  110.  
  111. print("Vettore aggiornato", myVector)
  112.  
  113. return
  114.  
  115.  
  116.  
  117. def svuotaArrayDinamico(myVector):
  118.  
  119. state=is_Empty_v2_0(myVector)
  120.  
  121. if (state==True):
  122. print ("Errore! il vettore è già vuoto!")
  123. else:
  124. nElementiDecrementata = len(myVector)
  125.  
  126. while(nElementiDecrementata>0):
  127. headElement=myVector[0]
  128. myVector.remove(headElement)
  129. nElementiDecrementata = nElementiDecrementata -1
  130.  
  131. print("Vettore aggiornato", myVector)
  132.  
  133. return
  134.  
  135.  
  136.  
  137. def svuotaArrayStatico(myVector):
  138. state = isEmpty_v2_0(myVector)
  139.  
  140. if (state == True):
  141. print ("L'array e' vuoto!")
  142.  
  143. return False
  144. else:
  145. lenght = len (myVector)
  146. for i in range (0, lenght):
  147. myVector[i]=sys.maxint
  148.  
  149. return True
  150.  
  151.  
  152. def clonaArray_conIterator(myVector):
  153. clonato=array('i',[])
  154.  
  155. for elemento in myVector:
  156. clonato.append(elemento)
  157.  
  158. return clonato
  159.  
  160.  
  161. def clonaArray_conIndice(myVector):
  162. clonato=array('i',[])
  163.  
  164. lunghezza=len(myVector)
  165. for i in range (0, lunghezza-1):
  166. elemento=myVector[i]
  167. clonato.append(elemento)
  168.  
  169. return clonato
  170.  
  171.  
  172. def eliminaUguali(myVector):
  173. newVector=array('i',[])
  174.  
  175. for elemento in myVector:
  176. if elemento not in newVector:
  177. newVector.append(elemento)
  178.  
  179. return newVector
  180.  
  181.  
  182. def isEmpty_v1_0(myVector):
  183. lunghezza=len(myVector)
  184.  
  185. if (lunghezza==0) :
  186. return 1
  187. else:
  188. return 0
  189.  
  190. def isEmpty_v1_1(myVector):
  191. lunghezza=myVector.count
  192.  
  193. if (lunghezza==0) :
  194. return 1
  195. else:
  196. return 0
  197.  
  198. def isEmpty_v2_0(myVector):
  199. trueOrFalse = ( len(myVector) == 0 )
  200.  
  201. return trueOrFalse
  202.  
  203. def isEmpty_v2_1(myVector):
  204. trueOrFalse = ( myVector.count == 0 )
  205.  
  206. return trueOrFalse
  207.  
  208. def isEmpty_v3_0(myVector):
  209. return ( len(myVector) == 0 )
  210.  
  211.  
  212. def isEmpty_v3_1(myVector):
  213. return ( myVector.count == 0 )
  214.  
  215.  
  216. def isFull(myVector):
  217. for index in range (0,len(myVector)):
  218. if (myVector[index]!= sys.maxint):
  219. continue
  220. else:
  221. return 0
  222.  
  223. def isFull_v2_0(myVector):
  224. for i in myVector:
  225. if (i != -1 and -1.0):
  226. a=True
  227. else:
  228. a=False
  229. break
  230. return a
  231.  
  232.  
  233. def isFull_v2_1(myVector):
  234. for i in myVector:
  235. if (i== -1 and -1.0):
  236. return False
  237. return True
  238.  
  239.  
  240. def separaElementi(myVector):
  241.  
  242. arrayPari=array("i",[])
  243. arrayDispari=array("i",[])
  244.  
  245. contaElementiPari=0
  246. contaElementiDispari=0
  247.  
  248. for element in myVector:
  249.  
  250. if ((element%2) == 0):
  251. arrayPari.append(element)
  252. else:
  253. arrayDispari.append(element)
  254.  
  255. return
  256.  
  257.  
  258. def scambiaOrdine(myVector):
  259.  
  260. lunghezza=len(myVector)
  261.  
  262. halfLenght=lunghezza/2
  263.  
  264. for i in range (0, halfLenght-1):
  265.  
  266. aux=myVector[i]
  267. myVector[i]=myVector[(lunghezza-1)-i]
  268. myVector[(lunghezza-1)-i]=aux
  269.  
  270. print "L'array invertito e': ", myVector
  271.  
  272. return
  273.  
  274.  
  275.  
  276. def shift_sinistra(myVector):
  277. headElement=myVector[0]
  278. myVector.remove(headElement)
  279. myVector.append(headElement)
  280.  
  281. print ("Vettore dopo lo shift a sinistra:", myVector)
  282. return 0
  283.  
  284. def shift_destra(myVector):
  285. numeroElementi=len(myVector)
  286.  
  287. tailElement=myVector[numeroElementi-1]
  288. myVector.remove(tailElement)
  289. myVector.insert(0,tailElement)
  290.  
  291. print ("Vettore dopo lo shift a destra:", myVector)
  292. return 0
  293.  
  294.  
  295. def rotazione_sx(myVector):
  296. for n_Elementi in myVector:
  297. shift_sinistra (myVector)
  298.  
  299. return 0
  300.  
  301.  
  302. def rotazione_dx(myVector):
  303. for n_Elementi in myVector:
  304. shift_destra (myVector)
  305.  
  306. return 0
  307.  
  308.  
  309. def shiftASinistra_C (myVector):
  310. l= len(myVector)
  311. headElement=myVector[0]
  312.  
  313. for i in range(0, l-1):
  314. myVector[i]=myVector[i+1]
  315.  
  316. myVector[l-1]=headElement
  317.  
  318. print ("Vettore shiftato a sx:", myVector)
  319.  
  320. return 0
  321.  
  322.  
  323. def shiftADestra_C(myVector):
  324. l= len(myVector)
  325. tailElement = myVector[l-1]
  326.  
  327. #Per scandire l'array in ordine inverso
  328. for i in xrange(l - 1, -1, -1):
  329. if (i<=l-1) :
  330. myVector[i]=myVector[i-1]
  331.  
  332. myVector[0]=tailElement
  333.  
  334. print ("Vettore shiftato a dx: ", myVector)
  335.  
  336. return 0
  337.  
  338.  
  339.  
  340. def selectionSort(myVector):
  341.  
  342. i=0
  343. j=0
  344. comodo=0
  345.  
  346.  
  347. n=len(myVector)
  348. for i in range(i, n-i):
  349. j=i+1
  350.  
  351. for j in range(j, n):
  352.  
  353. if(myVector[i]>myVector[j]):
  354. comodo=myVector[i]
  355. myVector[i]=myVector[j]
  356. myVector[j]=comodo
  357.  
  358.  
  359. return
  360.  
  361.  
  362. def bubbleSort(myVector):
  363.  
  364. N=len(myVector)
  365. Continua=True
  366.  
  367. while(Continua):
  368.  
  369. Continua=False
  370. for i in range(0, N-1):
  371. if(myVector[i]>myVector[i+1]):
  372. comodo=myVector[i]
  373. myVector[i]=myVector[i+1]
  374. myVector[i+1]=comodo
  375. Continua=True
  376.  
  377.  
  378. return
  379.  
  380.  
  381. def shakerSort(myVector):
  382.  
  383. numeroElementi= len(myVector)
  384. continua=True
  385. while continua==True:
  386. superiore=numeroElementi
  387. continua=False
  388. for i in range(0, superiore-1):
  389. if(myVector[i]>myVector[i+1]):
  390. aiuto=myVector[i]
  391. myVector[i]=myVector[i+1]
  392. myVector[i+1]=aiuto
  393.  
  394. continua=True
  395. superiore=i
  396.  
  397. for i in range(superiore-1, 0):
  398. if(myVector[i]>myVector[i-1]):
  399. aiuto=myVector[i]
  400. myVector[i]=myVector[i-1]
  401. myVector[i-1]=aiuto
  402.  
  403. continua=True
  404. superiore=i
  405. print myVector
  406. return
  407.  
  408.  
  409. def insertionSort(alist):
  410. for index in range(1,len(alist)):
  411.  
  412. currentvalue = alist[index]
  413. position = index
  414.  
  415. while position>0 and alist[position-1]>currentvalue:
  416. alist[position]=alist[position-1]
  417. position = position-1
  418.  
  419. alist[position]=currentvalue
  420.  
  421. def shellSort(alist):
  422. sublistcount = len(alist)//2
  423. while sublistcount > 0:
  424.  
  425. for startposition in range(sublistcount):
  426. gapInsertionSort(alist,startposition,sublistcount)
  427.  
  428. sublistcount = sublistcount // 2
  429.  
  430. def gapInsertionSort(alist,start,gap):
  431. for i in range(start+gap,len(alist),gap):
  432.  
  433. currentvalue = alist[i]
  434. position = i
  435.  
  436. while position>=gap and alist[position-gap]>currentvalue:
  437. alist[position]=alist[position-gap]
  438. position = position-gap
  439.  
  440. alist[position]=currentvalue
  441.  
  442. def RicercaBinaria(vettore, x):
  443.  
  444. n=len(vettore)
  445. primo = 0
  446. ultimo = n-1
  447. trovato = False
  448. indice=0
  449.  
  450. while (primo <= ultimo) and (not trovato):
  451. centro = (primo + ultimo) / 2
  452.  
  453. if (vettore[centro] == x):
  454. trovato = True
  455. indice=centro
  456.  
  457. print "L'elemento ricercato e' in posizione ", indice
  458.  
  459. elif (vettore[centro] < x):
  460. primo = centro + 1
  461.  
  462. else:
  463. ultimo = centro - 1
  464.  
  465. return trovato
  466.  
  467.  
  468.  
  469. #------------
  470. #Main program
  471. #------------
  472. def main():
  473. #Pulisce lo schermo
  474. os.system('clear')
  475.  
  476. numeroElementi=input("Inserisci il numero di elementi: ")
  477. staticVector = creaArrayIntStatico(numeroElementi)
  478.  
  479. #print ("Array di interi statico vuoto: ", staticVector)
  480. stampaArray(staticVector)
  481.  
  482. #dynamicVector = creaArrayIntDinamico()
  483.  
  484. #print ("Array di interi dinamico vuoto: ", dynamicVector)
  485. #riempiArrayDinamico(numeroElementi, vector)
  486.  
  487. riempiArrayStatico(staticVector)
  488. stampaArray(staticVector)
  489. #print ("Array riempito: ", staticVector)
  490.  
  491. #arrayClonato1= clonaArray_conIterator(vector)
  492. #print ("Array clonato con iteratore", arrayClonato1)
  493.  
  494. #arrayClonato2= clonaArray_conIndice(vector)
  495. #print ("Array clonato con indice", arrayClonato2)
  496.  
  497. #svuotaArray_conErrore(numeroElementi, vector)
  498. #svuotaArray(numeroElementi, vector)
  499.  
  500. #print ("Array svuotato: ", vector)
  501.  
  502.  
  503. if (__name__=="__main__"):
  504. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement