Python Hands-On Training

workspace.py
workspace.py
Python ile codingbat.com sitesi üzerinde pratik yaparken yazdığım birkaç fonksiyon.

def sleep_in(weekday, vacation): """ hafta içi değilse veya hafta sonu ise true dönen fonk. yaz sleep_in(False, False) → True sleep_in(True, False) → False sleep_in(False, True) → True """ if weekday == False or vacation: return True return False # return(not weekday or vacation) # this is shortest version
def monkey_trouble(a_smile, b_smile): """ iki maymun var a,b. ikisi gülünce veya her ikisi susunca sıkıntıdayız. Sıkıntıda isek True dönen fonk. yazınız monkey_trouble(True, True) → True monkey_trouble(False, False) → True monkey_trouble(True, False) → False """ return (a_smile and b_smile) or (not a_smile and not b_smile)
def sum_double(a, b): """ iki sayı veriliyor aynı iseler toplamın 2 katını değilse toplamını döndür. sum_double(1, 2) → 3 sum_double(3, 2) → 5 sum_double(2, 2) → 8 """ if a == b: return (a + b) * 2 return a + b
def diff21(n): """ 21'den küçük sayılar için verilen n ile 21 'in farkını 21'den büyük sayılar için verilen n ile 21'in farkının 2katını döndür diff21(19) → 2 diff21(10) → 11 diff21(21) → 0 """ if n <= 21: return 21 - n return (n - 21) * 2
def parrot_trouble(talking, hour): """ konuşan papağanımız var. saat 7'den önce veya 20'den sonra ise ve papağan konuşuyorsa başımız belada :). başımız belada ise True dönen fonk. yaz. parrot_trouble(True, 6) → True parrot_trouble(True, 7) → False parrot_trouble(False, 6) → False """ return (talking and (hour < 7 or hour > 20))
def makes10(a, b): """ iki integer değer veriliyor. ikisinden biri 10 ise veya toplamları 10 ise True dönsün makes10(9, 10) → True makes10(9, 9) → False makes10(1, 9) → True """ return (a == 10 or b == 10) or (a + b) == 10
def near_hundred(n): """ int n değeri veriliyor. 100'ün katlarına 10 br yakınsa true dönen fonk. yaz near_hundred(93) → True near_hundred(90) → True near_hundred(89) → False """ return (abs(100 - n) <= 10) or (abs(200 - n) <= 10)
def pos_neg(a, b, negative): """ iki integer değer veriliyor farklı iseler True döndür ancak "negative" True ise olmaz o zaman ikisi de negatif ise True döndür. pos_neg(1, -1, False) → True pos_neg(-1, 1, False) → True pos_neg(-4, -5, True) → True """ return (negative and (a < 0 and b < 0)) or \ (not negative and ((a < 0 and b > 0) or (a > 0 and b < 0)))
def not_string(str): """ bir string veriliyor. eğer "not" ie başlıyorsa stringi değişmeden döndür. eğer not ile başlamıyorsa başına "not" ekle döndür. not_string('candy') → 'not candy' not_string('x') → 'not x' not_string('not bad') → 'not bad' """ if len(str) >= 3: head = str[0] + str[1] + str[2] if head == "not": return str return "not " + str
def missing_char(str, n): """ bir string veriliyor ve bir index. bu index'in olduğu yerdeki değeri sil front = str[:n] # up to but not including n back = str[n+1:] # n+1 through end of string return front + back missing_char('kitten', 1) → 'ktten' missing_char('kitten', 0) → 'itten' missing_char('kitten', 4) → 'kittn' """ if n > -1: return str.replace(str[n], "")
def front_back(str): """ verilen stringin ilk ve son karakterlerini değişen fonksiyonu yazınız front_back('code') → 'eodc' front_back('a') → 'a' front_back('ab') → 'ba' """ if len(str) < 2: return str else: f = str[0] l = str[-1] a = "" for i in range(1, len(str) - 1, 1): a = a + str[i] return l + a + f
def front3(str): """ verilen string uzunlugu 3 den buyuk ise ilk 3 hafrini 3 kere yaz degilse aynı stringi 3 kere döndür front3('Java') → 'JavJavJav' front3('Chocolate') → 'ChoChoCho' front3('abc') → 'abcabcabc' """ if len(str) >= 3: str = str[0:3] return str + str + str return str + str + str
def string_times(str, n): """ verilen stringi verilen integer degeri kadar kopyala dönder string_times('Hi', 2) → 'HiHi' string_times('Hi', 3) → 'HiHiHi' string_times('Hi', 1) → 'Hi' """ s = "" for i in range(0, n): s = s + str return s
def front_times(str, n): """ string veriliyor, ilk 3 karaktterini verilen integer n degerin kadar kopyala yaz front_times('Chocolate', 2) → 'ChoCho' front_times('Chocolate', 3) → 'ChoChoCho' front_times('Abc', 3) → 'AbcAbcAbc' """ a = "" str = str[:3] for i in range(n): a = a + str return a
def string_bits(str): """ string veriliyor , tek indexli karakterleri gözardı et string_bits('Hello') → 'Hlo' string_bits('Hi') → 'H' string_bits('Heeololeo') → 'Hello' """ a = "" for i in range(len(str)): if (i % 2) == 0: a = a + str[i] return a
def string_splosion(str): """ string veriliyor ilk karakterle birlikte artacak şekilde dönen string fonk yaz string_splosion('Code') → 'CCoCodCode' string_splosion('abc') → 'aababc' string_splosion('ab') → 'aab' """ r = "" t = "" for i in range(len(str)): r = r + str[i] t = t + r return t
def last2(str): """ verilen bir string'te son iki karakter kaç defa geçiyor bunu bul. last2('hixxhi') → 1 last2('xaxxaxaxx') → 1 last2('axxxaaxx') → 2 """ if len(str) < 3: return 0 else: count = 0 s = "" l2 = str[len(str) - 2:] # son iki karkteri al for i in range(0, len(str) - 2): s = str[i] + str[i + 1] if s == l2: count = count + 1 return count
def array_count9(nums): """ verilen intger dizideki 9 kaç defa geçmiştir ? array_count9([1, 2, 9]) → 1 array_count9([1, 9, 9]) → 2 array_count9([1, 9, 9, 3, 9]) → 3 """ if len(nums) > 0: c = 0 for i in nums: if i == 9: c = c + 1 return c else: return 0
def array_front9(nums): """ verilen integer dizide ilk 4 elemanda 9 varsa True dönder array_front9([1, 2, 9, 3, 4]) → True array_front9([1, 2, 3, 4, 9]) → False array_front9([1, 2, 3, 4, 5]) → False """ for i in nums[:4]: if i == 9: return True return False
def array123(nums): """ verilen integer dizisinde 1,2,3 ard arda geliyorsa True dönen fonk yaz. Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere. array123([1, 1, 2, 3, 1]) → True array123([1, 1, 2, 4, 1]) → False array123([1, 1, 2, 1, 2, 3]) → True """ if len(nums) >= 3: for i in range(len(nums) - 2): if nums[i] == 1 and nums[i + 1] == 2 and nums[i + 2] == 3: return True return False
def string_match(a, b): """ iki adet string veriliyor, iki string içinde de ard arda gelen 2 alt-string aynı pozisyonda ise bunların kaç adet olduğunu söyleyen fonk. yaz string_match('xxcaazz', 'xxbaaz') → 3 string_match('abc', 'abc') → 2 string_match('abc', 'axc') → 0 """ count = 0 lim = len(a) - 1 if len(a) < len(b) else len(b) - 1 # küçük olanı almalıyım if lim >= 1: for i in range(lim): if (a[i] + a[i + 1]) == (b[i] + b[i + 1]): count = count + 1 return count
def hello_name(name): """ Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!". hello_name('Bob') → 'Hello Bob!' hello_name('Alice') → 'Hello Alice!' hello_name('X') → 'Hello X!' """ if len(name) > 0: return "Hello " + name + "!"
def make_abba(a, b): """ Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". make_abba('Hi', 'Bye') → 'HiByeByeHi' make_abba('Yo', 'Alice') → 'YoAliceAliceYo' make_abba('What', 'Up') → 'WhatUpUpWhat' """ return a + b + b + a
def make_tags(tag, word): return "<" + tag + ">" + word + "</" + tag + ">"
def make_out_word(out, word): """ Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<<word>>". make_out_word('<<>>', 'Yay') → '<<Yay>>' make_out_word('<<>>', 'WooHoo') → '<<WooHoo>>' make_out_word('[[]]', 'word') → '[[word]]' """ return out[:2] + word + out[2:]
def extra_end(str): """ Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2. extra_end('Hello') → 'lololo' extra_end('ab') → 'ababab' extra_end('Hi') → 'HiHiHi' """ if len(str) >= 2: return str[-2:] + str[-2:] + str[-2:] return str
def first_two(str): """ Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "". first_two('Hello') → 'He' first_two('abcdefg') → 'ab' first_two('ab') → 'ab' """ if len(str) >= 2: return str[:2] return str
def first_half(str): """ Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". first_half('WooHoo') → 'Woo' first_half('HelloThere') → 'Hello' first_half('abcdef') → 'abc' """ return str[:len(str) / 2]
def without_end(str): """ uzunluğu en az 2 karakter olan bir stringin son ve baş karakteri hariç diğer karakterleri döndüren fonk yaz without_end('Hello') → 'ell' without_end('java') → 'av' without_end('coding') → 'odin' """ if len(str) >= 2: return str[1:len(str) - 1] return str
def combo_string(a, b): """ Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). combo_string('Hello', 'hi') → 'hiHellohi' combo_string('hi', 'Hello') → 'hiHellohi' combo_string('aaa', 'b') → 'baaab' """ if len(a) != len(b): big = a if len(a) > len(b) else b small = a if len(a) < len(b) else b return small + big + small
def non_start(a, b): """ iki tane string veriliyor ilk karakterler olmadan birlestir dönder non_start('Hello', 'There') → 'ellohere' non_start('java', 'code') → 'avaode' non_start('shotl', 'java') → 'hotlava' """ if len(a) > 0 and len(b) > 0: return a[1:] + b[1:]
def left2(str): """ Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end. The string length will be at least 2. left2('Hello') → 'lloHe' left2('java') → 'vaja' left2('Hi') → 'Hi' """ if len(str) > 1: return str[2:] + str[:2]
def first_last6(nums): """ Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more. first_last6([1, 2, 6]) → True first_last6([6, 1, 2, 3]) → True first_last6([13, 6, 1, 2, 3]) → False """ if len(nums) > 0: return (nums[0] == 6) or (nums[-1] == 6) return False
def same_first_last(nums): """ integer dizi veriliyor boyutu en az 1 olacak, eger ilk ve son eleman esit ise True dönder same_first_last([1, 2, 3]) → False same_first_last([1, 2, 3, 1]) → True same_first_last([1, 2, 1]) → True """ if len(nums) > 0: return nums[0] == nums[-1] return False
def make_pi(): ar = [3, 1, 4] return ar
def common_end(a, b): """ Given 2 arrays of ints, a and b, return True if they have the same first element or they have the same last element. Both arrays will be length 1 or more. common_end([1, 2, 3], [7, 3]) → True common_end([1, 2, 3], [7, 3, 2]) → False common_end([1, 2, 3], [1, 3]) → True """ if len(a) > 0 and len(b) > 0: return a[0] == b[0] or a[-1] == b[-1]
def sum3(nums): """ 3 elemanlı dizi veriliyor toplamlarını döndür sum3([1, 2, 3]) → 6 sum3([5, 11, 2]) → 18 sum3([7, 0, 0]) → 7 """ return nums[0] + nums[1] + nums[2]
def rotate_left3(nums): """ 3 elemanlı integer dizisinde her elemantı sola kaydır rotate_left3([1, 2, 3]) → [2, 3, 1] rotate_left3([5, 11, 9]) → [11, 9, 5] rotate_left3([7, 0, 0]) → [0, 0, 7] """ tmp = nums[0] nums[0] = nums[1] nums[1] = nums[2] nums[2] = tmp return nums
def reverse3(nums): """ Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. reverse3([1, 2, 3]) → [3, 2, 1] reverse3([5, 11, 9]) → [9, 11, 5] reverse3([7, 0, 0]) → [0, 0, 7] """ tmp = nums[0] nums[0] = nums[2] nums[2] = tmp return nums
def max_end3(nums): """ ilk eleman mı son eleman mı buyuk bunu bul sonra tum elmanlara bunu ata(assign) max_end3([1, 2, 3]) → [3, 3, 3] max_end3([11, 5, 9]) → [11, 11, 11] max_end3([2, 11, 3]) → [3, 3, 3] """ m = nums[0] if nums[0] > nums[-1] else nums[-1] nums[0] = m nums[1] = m nums[2] = m return nums
def sum2(nums): """ verlen bir integer dizisinde ilk 2 elemanın toplamını döndür dizi boyutu 2'den küçük ise direkt topla döndür sum2([1, 2, 3]) → 3 sum2([1, 1]) → 2 sum2([1, 1, 1, 1]) → 2 """ if len(nums) < 2: return sum(nums) return nums[0] + nums[1]
def middle_way(a, b): """ Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. middle_way([1, 2, 3], [4, 5, 6]) → [2, 5] middle_way([7, 7, 7], [3, 8, 0]) → [7, 8] middle_way([5, 2, 9], [1, 4, 5]) → [2, 4] """ return [a[1]] + [b[1]]
def make_ends(nums): """ Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more. make_ends([1, 2, 3]) → [1, 3] make_ends([1, 2, 3, 4]) → [1, 4] make_ends([7, 4, 6, 2]) → [7, 2] """ if len(nums) > 0: return [nums[0]] + [nums[-1]]
def has23(nums): """ dizi 2 veya 3 içeriyor mu ? has23([2, 5]) → True has23([4, 3]) → True has23([4, 5]) → False """ return (2 in nums) or (3 in nums)
def cigar_party(cigars, is_weekend): """ haftasonu üst sınır yok cigar(puro)'larda hafta sonu degilse 40-60 arası olmalı cigar_party(30, False) → False cigar_party(50, False) → True cigar_party(70, True) → True When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise. """ if is_weekend and cigars >= 40: return True elif (cigars <= 60 and cigars >= 40) and not is_weekend: return True else: return False
def date_fashion(you, date): """ sen ve arkadaşın restorana gidiyorsunuz. ikinizin de şıklık derecesi 0-10 arasında 0=no, 1=maybe, 2=yes olarak tabloda derecelendiriliyorsunuz. Eğer ikinizden biriniz çok şık iseniz, 8 veya daha fazla, sonuç 2(yes) dönecek Eğer ikinizden birinin derecesi 2 veya daha az ise 0(no) dönecek Diğer durumlarda sonuç 1(maybe) dönecek. date_fashion(5, 10) → 2 date_fashion(5, 2) → 0 date_fashion(5, 5) → 1 """ if (you >= 8 or date >= 8) and (not you <= 2 and not date <= 2): return 2 elif you <= 2 or date <= 2: return 0 else: return 1
def squirrel_play(temp, is_summer): """ The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise. squirrel_play(70, False) → True squirrel_play(95, False) → False squirrel_play(95, True) → True """ if is_summer and temp <= 100 and temp >= 60: return True elif not is_summer and temp <= 90 and temp >= 60: return True else: return False
def caught_speeding(speed, is_birthday): """ You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases. caught_speeding(60, False) → 0 caught_speeding(65, False) → 1 caught_speeding(65, True) → 0 """ if is_birthday: if speed <= (60 + 5): return 0 elif speed > (60 + 5) and speed <= (80 + 5): return 1 else: return 2 else: if speed <= 60: return 0 elif speed > 60 and speed <= 80: return 1 else: return 2
def sorta_sum(a, b): """ Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20. sorta_sum(3, 4) → 7 sorta_sum(9, 4) → 20 sorta_sum(10, 11) → 21 """ if (a + b) >= 10 and (a + b) < 20: return 20 else: return a + b
def alarm_clock(day, vacation): """ günler = [0,1,2,3,4,5,6] veriliyor 0:pazar 6:cumartesi Eğer tatilde isek haftasonu ise "off", hafta içi ise 10:00 döndür Eğer tatilde değilsek haftasonu 10:00 hafta içi 7:00 döndür alarm_clock(1, False) → '7:00' alarm_clock(5, False) → '7:00' alarm_clock(0, False) → '10:00' """ if vacation: if day == 0 or day == 6: return "off" else: return "10:00" else: if day == 0 or day == 6: return "10:00" else: return "7:00"
def love6(a, b): """ verilen iki değerden biri 6 ise veya toplamları 6 ise veya farkları 6 ise True döndür love6(6, 4) → True love6(4, 5) → False love6(1, 5) → True """ return a == 6 or b == 6 or (a + b) == 6 or (a - b) == 6 or (b - a) == 6
def in1to10(n, outside_mode): """ integer n değeri veriliyor 1-10 arasında, bunlar dahil outside_mode, True ise sayı 1'e eşit ve küçükse veya 10'a eşit ve buyukse True dönder outside_mode False ise sayı 1-10 arasında olup olmadığına bak in1to10(5, False) → True in1to10(11, False) → False in1to10(11, True) → True """ if outside_mode: return n <= 1 or n >= 10 else: return n > 0 and n < 11
def near_ten(num): """ Eğer sayımız 10 ve 10'un katlarına 2 br yakınsa True dönder near_ten(12) → True near_ten(17) → False near_ten(19) → True """ near = 10 - (num % 10) if near in [0, 1, 2, 8, 9, 10]: return True else: return False

Yorumlar