CodingBat Solutions in Python

 

big-diff

Question:
Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values.
big_diff([10, 3, 5, 6]) → 7
big_diff([7, 2, 10, 9]) → 8
big_diff([2, 10, 7, 2]) → 8

Answer:
def big_diff(nums):
return(max(nums)-min(nums))

count-evens

Question:
Return the number of even ints in the given array. Note: the % “mod” operator computes the remainder, e.g. 5 % 2 is 1.
count_evens([2, 1, 2, 3, 4]) → 3
count_evens([2, 2, 0]) → 3
count_evens([1, 3, 5]) → 0

Answer:
def count_evens(nums):
a = 0
for i in nums:
if i%2 == 0:
a+=1
return(a)

common_end

Question:
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

Answer:
def common_end(a, b):
if a[0] == b[0] or a[-1] == b[-1]:
return True
else:
return False

first_last6

Question:
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

Answer
def first_last6(nums):

if nums[0] == 6 or nums[-1] == 6:
return True
else:
return False

has23

Question:
Given an int array length 2, return True if it contains a 2 or a 3.
has23([2, 5]) → True
has23([4, 3]) → True
has23([4, 5]) → False

Answer:
def has23(nums):
for i in nums:
if i in(2,3):
return True
return False

make_ends

Question:
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]

Answer:
def make_ends(nums):
arr = []

arr.append(nums[0])
arr.append(nums[-1])
return(arr)

middle_way

Question:
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]

Answer:
def middle_way(a, b):
new_arr = []

new_arr.append(a[1])
new_arr.append(b[1])
return(new_arr)

reverse3

Question:
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]

Answer:
def reverse3(nums):

return(nums[::-1])

same_first_last

Question:
Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal.
same_first_last([1, 2, 3]) → False
same_first_last([1, 2, 3, 1]) → True
same_first_last([1, 2, 1]) → True

Answer:
def same_first_last(nums):

if len(nums) >=1 and nums[0] == nums[-1]:
return True
else:
return False

sum2

Question:
Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.
sum2([1, 2, 3]) → 3
sum2([1, 1]) → 2
sum2([1, 1, 1, 1]) → 2

Answer:
def sum2(nums):
if len(nums) < 2: return(sum(nums)) else: return(sum(nums[0:2]))

sum3

Question:
Given an array of ints length 3, return the sum of all the elements.
sum3([1, 2, 3]) → 6
sum3([5, 11, 2]) → 18
sum3([7, 0, 0]) → 7

Answer:
def sum3(nums):

return(sum(nums))

cigar_party

Question:
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.
cigar_party(30, False) → False
cigar_party(50, False) → True
cigar_party(70, True) → True

Answer:
def cigar_party(cigars, is_weekend):

if cigars >= 40 and cigars <= 60: return True elif cigars >= 40 and is_weekend == True:
return True
else:
return False

in1to10

Question:
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
in1to10(5, False) → True
in1to10(11, False) → False
in1to10(11, True) → True

Answer:
def in1to10(n, outside_mode):
if outside_mode == False:
if n >= 1 and n <= 10: return True else: return False elif outside_mode == True: if n <= 1 or n >= 10:
return True
return False

love6

Question:
The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.
love6(6, 4) → True
love6(4, 5) → False
love6(1, 5) → True

Answer:
def love6(a, b):
if a == 6 or b == 6:
return True
elif a+b == 6:
return True
elif a-b == 6 or a-b == -6:
return True
else:
return False

sorta_sum

Question:
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

Answer:
def sorta_sum(a, b):
c = a+b
if c >=10 and c <=19: return(20) return(c)

cat-dog

Question:
Return True if the string "cat" and "dog" appear the same number of times in the given string.
cat_dog('catdog') → True
cat_dog('catcat') → False
cat_dog('1cat1cadodog') → True

Answer:
def cat_dog(str):
c = str.count("cat")
d = str.count("dog")
if c == d:
return True
return False

count-code

Question:
Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.
count_code('aaacodebbb') → 1
count_code('codexxcode') → 2
count_code('cozexxcope') → 2

Answer:
def count_code(str):
count = 0
i=0
while "co" in str[i:]:
if len(str[i+str[i:].index("co"):]) >= 4 and str[i+3+str[i:].index("co")] == "e":
count += 1
i += str[i:].index("co")+3
return count

count-hi

Question:
Return the number of times that the string "hi" appears anywhere in the given string.
count_hi('abc hi ho') → 1
count_hi('ABChi hi') → 2
count_hi('hihi') → 2

Answer:
def count_hi(str):
res = str.count("hi")
return(res)

double-char

Question:
Given a string, return a string where for every char in the original, there are two chars.
double_char('The') → 'TThhee'
double_char('AAbb') → 'AAAAbbbb'
double_char('Hi-There') → 'HHii--TThheerree'

Answer:
def double_char(str):
return ''.join(char * 2 for char in str)

extra_end

Question:
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'

Answer:
def extra_end(str):

return(str[-2:]*3)

first_half

Question:
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'

Answer:
def first_half(str):
v = len(str)/2

return(str[0:v])

first_two

Question:
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'

Answer:
def first_two(str):
if len(str) < 2: return(str) else: return(str[0:2])

hello_name

Question:
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!'

Answer:
def hello_name(name):

return("{}{}{}".format("Hello ", name, "!"))

make_abba

Question:
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'

Answer:
def make_abba(a, b):
return("{}{}{}{}".format(a,b,b,a))

make_tags

Question:
The web is built with HTML strings like "Yay" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word, e.g. "Yay".
make_tags('i', 'Yay') → 'Yay'
make_tags('i', 'Hello') → 'Hello'
make_tags('cite', 'Yay') → 'Yay'

Answer:
def make_tags(tag, word):
i = "<" b = ">"
c = "/"
return("{}{}{}{}{}{}{}{}".format(i,tag,b,word,i,c,tag,b))

without_end

Question:
Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length will be at least 2.
without_end('Hello') → 'ell'
without_end('java') → 'av'
without_end('coding') → 'odin'

Answer:
def without_end(str):
return(str[1:-1])