BAZI RNEK PYTHON KODLARI split from index k
BAZI ÖRNEK PYTHON KODLARI
split from index k and add first part of arr to the end def split. Arr(arr, n, k): for i in range(n-1, k-1, -1): x = arr[n-1] for j in range(n-1, 0, -1): arr[j] = arr[j - 1] arr[0] = x # main arr = [12, 10, 5, 6, 52, 36] n = len(arr) position = 2 split. Arr(arr, n, position) for i in range(0, n): print(arr[i], end = ' ') # Calculates the first n Fibonacci numbers n = 100 # Keep track of the two most recent Fibonacci numbers a, b = 1, 1 print(a, b, end='') for i in range(2, n+1): # The next number (b) is a+b, and a becomes the previous b a, b = b, a+b print(' ', b, end='')
Euclid's algorithm for finding the gcd of a number >>> a, b = 1071, 462 >>> while b: . . . a, b = b, a % b >>> print(a) def Sieve. Of. Eratosthenes(n): # Create a boolean array "prime[0. . n]" and initialize all entries it as true. A value in prime[i] will finally be false if i is Not a prime, else true. prime = [True for i in range(n+1)] p = 2 while (p * p <= n): # If prime[p] is not changed, then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, n+1, p): prime[i] = False p += 1 # Print all prime numbers for p in range(2, n): if prime[p]: print(p) print(Sieve. Of. Eratosthenes(50))
SAMPLE EXAM QUESTIONS Aşağıdaki python fonksiyonu iki listeyi argüman olarak alıp aralarındaki farklı elemanların listesini geri döndürecektir. Eksik kalan satırı yazınız. def Diff(li 1, li 2): li_dif=[] for i in li 1+li 2: if i not in li 1 or i not in li 2: li_dif. append(i) return li_dif Aşağıdaki python kodu ekrana ne basar? def func(li, n): li 2=[] for i in range(len(li)): if(li[i]==n): li 2. append(i) return li 2 test_list = [1, 3, 4, 3, 6, 7, 9, 3, 2, 5] print ("Result is : " + str(func(test_list, 3))) Result is : [1, 3, 7] basar.
def spam() : eggs = 'spam' print(eggs) def bacon() : eggs = 'bacon' print(eggs) spam() print(eggs) eggs = 'global' bacon() print(eggs) flip_flop = True increment = 0 number = 6 while number > 1 : if flip_flop == True : increment = 2 flip_flop = False else : increment = -3 flip_flop = True number = number + increment print(number)
def spam(number) : if (number[0] % 2) == 0 : number[0] = number[0] // 4 print(number[0]) def spam(number) : if (number % 2) == 0 : number = number // 4 print(number) def bacon(number) : if (number[0] % 2) != 0 : number[0] = 5 * number[0] + 1 print(number[0]) def bacon(number) : if (number % 2) != 0 : number = 5 * number + 1 print(number) number = [30] while number[0] > 1 : bacon(number) spam(number) number = 30 while number > 1 : bacon(number) spam(number)
def is. Palindrome(s): return s == s[: : -1] # Driver code s = "malayalam" ans = is. Palindrome(s) if ans: print("Yes") else: print("No")
>>> scores = [87, 75, 50, 32] >>> ranks = [] >>> for score in scores: . . . ranks. append(scores. index(score) + 1) . . . >>> ranks [1, 2, 2, 4, 5, 5]
def remove. Duplicate(str): s=set(str) s="". join(s) print("Without Order: ", s) t="" for i in str: if(i in t): pass else: t=t+i print("With Order: ", t) str="geeksforgeeks" remove. Duplicate(str)
def uncommon(A, B): un_comm = [i for i in B. split() if i not in A. split()] return un_comm #Driver code A = "Geeks for Geeks" B = "Learning from Geeks for Geeks" print(uncommon(A, B))
# initi al izi ng string test _str = 'Geeksforgeeks is best for geeks and CS' # p rint ing original string print (" The original string is : " + str(test_str)) # initi al izi ng word list word_l ist = ["best", 'C S', 'for'] # initi al izi ng replace wor d rep l_w rd = 'gfg' # Re pl ace multiple words w ith K # Using joi n() + split() + list comprehension res = ' '. joi n([repl_wrd if idx in word_list else idx for id x in test _str. spli t()]) # p rint ing result print (" String after mult iple replace : " + str(res))
de f che ck. E mpty(input, p att ern): # I f both are empty if le n(input)== 0 and len(pattern)== 0: re turn 'true ' # If onl y pattern is em pty if le n(pattern)== 0: re turn 'true ' whi le (len(input) != 0 ): # fi nd sub-string in main string index = input. find(pat tern) # check if sub-string founded or not if (i ndex ==(-1)): return 'false ' # sl ice input string in t wo parts and concatenate input = input[0: in dex ] + input[index + len(pattern) : ] re t urn 'true' inp ut = 'GE EGEEKSKS' patte rn = 'GEEKS' print (check. Empty(inpu t, pa ttern))
# To find minimum sum of product of number def find. Min. Sum(num): sum = 0 # Find factors of number and add to the sum i = 2 while(i * i <= num): while(num % i == 0): sum += i num /= i i += 1 sum += num # Return sum of numbers having minimum product return sum # Driver Code num = 12 print find. Min. Sum(num)
def hailstone(n): count = 1 """Print the terms of the 'hailstone sequence' from n to 1. """ assert n > 0 print(n) if n > 1: if n % 2 == 0: count += hailstone(n / 2) else: count += hailstone((n * 3) + 1) return count result = hailstone(10) print(result)
#Function to left rotate arr[] of size n by d def left. Rotate(arr, d, n): for i in range(gcd(d, n)): # move i-th values of blocks temp = arr[i] j = i while 1: k = j + d if k >= n: k = k - n if k == i: break arr[j] = arr[k] j = k arr[j] = temp #Function to get gcd of a and b def gcd(a, b): if b == 0: return a; else: return gcd(b, a%b) # Driver program to test above functions arr = [1, 2, 3, 4, 5, 6] left. Rotate(arr, 2, 6) print(arr, end=" ")
# Function to do insertion sort def insertion. Sort(arr): # Traverse through 1 to len(arr) for i in range(1, len(arr)): key = arr[i ] # Move elements of arr[0. . i-1], that are greater than key, to one position ahead of their current position j = i-1 while j >= 0 and key < arr[j] : arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key # Driver code to test above arr = [12, 11, 13, 5, 6] insertion. Sort(arr) for i in range(len(arr)): print ("% d" % arr[i ])
- Slides: 16