← coderbyte_practice

the rest

8311fa8fbb22169ccd13682219c6aac9b6f5851d · ovidiucs · May 6, 2015 20:44

+305 · 14 files changed

  • easy/16-arith-geo.py +26 −0
  • easy/17-array-addition-1.py +23 −0
  • easy/18-letter-count-1.py +29 −0
  • easy/19-second-great-low.py +11 −0
  • easy/20-division-stringified.py +15 −0
  • easy/21-counting-minutes-1.py +48 −0
  • easy/22-mean-mode.py +10 −0
  • easy/23-dash-insert.py +29 −0
  • easy/24-swap-case.py +13 −0
  • easy/25-number-addition.py +28 −0
  • easy/26-third-greatest.py +12 −0
  • easy/27-powers-of-two.py +10 −0
  • easy/28-additive-persistance.py +24 −0
  • easy/29-multiplicative-persistance.py +27 −0
diff --git a/easy/16-arith-geo.py b/easy/16-arith-geo.py
index e69de29..a126177 100644
--- a/easy/16-arith-geo.py
+++ b/easy/16-arith-geo.py
@@ -0,0 +1,26 @@
+def ArithGeo(arr):
+    diff = arr[1] - arr[0]
+    div = float(arr[1]) / float(arr[0])
+    for index in range(len(arr)-1):
+        if not (arr[index + 1] - arr[index] == diff):
+            for index2 in range(len(arr)-1):      
+                if not float((arr[index2 + 1]) / float(arr[index2]) == div):
+                    return -1
+            return 'Geometric'
+    return 'Arithmetic'
+
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print ArithGeo(raw_input())           
+"""
+Have the function ArithGeo(arr) take the array of numbers stored in arr
+and return the string "Arithmetic" if the sequence follows an arithmetic
+pattern or return "Geometric" if it follows a geometric pattern. 
+If the sequence doesn't follow either pattern return -1. 
+An arithmetic sequence is one where the difference between 
+each of the numbers is consistent, where as in a geometric sequence, 
+each term after the first is multiplied by some constant or common ratio. 
+Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54]. 
+Negative numbers may be entered as parameters, 0 will not be entered,
+ and no array will contain all the same elements. 
+"""
diff --git a/easy/17-array-addition-1.py b/easy/17-array-addition-1.py
index e69de29..025421a 100644
--- a/easy/17-array-addition-1.py
+++ b/easy/17-array-addition-1.py
@@ -0,0 +1,23 @@
+def ArrayAdditionI(arr): 
+    targetValue = max(arr)
+    arr.remove(targetValue)
+    mall = sum(arr)
+    if targetValue <= mall:
+       return 'true'
+    else:
+       return 'false'
+
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print ArrayAdditionI(raw_input())           
+"""
+Have the function ArrayAdditionI(arr) 
+take the array of numbers stored in arr
+ and return the string true if any combination 
+ of numbers in the array can be added up to equal
+the largest number in the array, otherwise return 
+the string false. For example: if arr contains [4, 6, 23, 10, 1, 3]
+the output should return true because 4 + 6 + 10 + 3 = 23. 
+The array will not be empty, will not contain all
+the same elements, and may contain negative numbers. 
+"""
diff --git a/easy/18-letter-count-1.py b/easy/18-letter-count-1.py
index e69de29..5b5f70f 100644
--- a/easy/18-letter-count-1.py
+++ b/easy/18-letter-count-1.py
@@ -0,0 +1,29 @@
+def LetterCountI(str):
+    al = str.split()
+    alp = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+    le = []
+
+    for index, word in enumerate(al):
+        for letter in alp:
+            if word.count(letter) > 1:
+                le.append(index)
+    if le == []:
+        return -1
+
+    return al[le[0]]
+
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print LetterCountI(raw_input())           
+"""
+Have the function LetterCountI(str)
+take the str parameter being passed
+and return the first word with the greatest
+number of repeated letters.
+For example: "Today, is the greatest day ever!"
+should return greatest because it has 2 e's
+(and 2 t's) and it comes before ever which also
+has 2 e's. If there are no words with repeating
+letters return -1. Words will be separated by spaces. 
+"""
+
diff --git a/easy/19-second-great-low.py b/easy/19-second-great-low.py
index e69de29..6cab76e 100644
--- a/easy/19-second-great-low.py
+++ b/easy/19-second-great-low.py
@@ -0,0 +1,11 @@
+def SecondGreatLow(arr): 
+  alt = sorted(list(set(arr)))
+  if len(alt) == 1:
+    alt.append(alt[0])
+  return ' '.join([str(alt[x]) for x in 1,-2])
+    
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print SecondGreatLow(raw_input())           
+
diff --git a/easy/20-division-stringified.py b/easy/20-division-stringified.py
index e69de29..e40e0a3 100644
--- a/easy/20-division-stringified.py
+++ b/easy/20-division-stringified.py
@@ -0,0 +1,15 @@
+from math import ceil
+
+def DivisionStringified(num1,num2): 
+
+  resultFloat = num1/float(num2)
+  resultFloatInt = "{:,}".format(int(resultFloat // 1))
+  if resultFloat <= 999:
+    return int(ceil(resultFloat))
+  else:
+  	return resultFloatInt
+
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print DivisionStringified(raw_input())       
+# not working
diff --git a/easy/21-counting-minutes-1.py b/easy/21-counting-minutes-1.py
index e69de29..7dc3b50 100644
--- a/easy/21-counting-minutes-1.py
+++ b/easy/21-counting-minutes-1.py
@@ -0,0 +1,48 @@
+def CountingMinutesI(str):
+
+    e = str.replace(':',' ').replace('-',' ').replace('pm',' p').replace('am',' a').split()
+    g = [int(s) for s in e if s.isdigit()]
+
+    if (e[2] == 'p' and e[5] == 'p') or (e[2] == 'a' and e[5] == 'a'):
+
+        if g[0] == g[2]:
+            if g[1] == g[3]:
+                return 1440
+            
+            elif g[1] < g[3]:
+                return abs((g[1])-g[3])
+            
+            elif g[1] > g[3]:
+                return 1380+(60-abs((g[1])-g[3]))
+        
+        elif g[0] < g[2]:
+            if g[2] == 12:
+                if g[3] < g[1]:
+                    return 720+((g[2]-g[0])*60)-(abs(g[1]-g[3]))
+                return 720+((g[2]-g[0])*60)+(abs(g[1]-g[3]))
+            return abs((g[1])-g[3])+60*abs((g[2])-g[0])
+
+        elif g[0] > g[2]:
+            if g[0] == 12:
+                if g[1] == g[3] or g[1] < g[3]:
+                    return abs((g[1])-g[3])+60*g[2]            
+            return (g[2]*60)+abs((g[1])-g[3])
+
+    elif e[2] == 'a' and e[5] == 'p' or e[2] == 'p' and e[5] == 'a':
+        if g[0] == 12 or g[2] == 12:
+            if g[2] == 12:
+                return 720-(g[0]*60)+abs(g[1]-g[3])
+            return 720+(g[2]*60)+abs(g[1]-g[3])
+        elif g[0] == g[2]:
+            if g[3] == g[1]:
+                return 720
+            elif g[3] > g[1]:
+                return 720+abs((g[1])-g[3])
+            elif g[3] < g[1]:
+                return 720-abs((g[1])-g[3])
+        elif g[0] < g[2]:
+            return 720+abs((g[2] - g[0])*60)+abs(g[1]-g[3])
+        
+        return ((g[1])-g[3])+720
+print CountingMinutesI(raw_input())           
+# needs redoing
diff --git a/easy/22-mean-mode.py b/easy/22-mean-mode.py
index e69de29..d6ae59a 100644
--- a/easy/22-mean-mode.py
+++ b/easy/22-mean-mode.py
@@ -0,0 +1,10 @@
+def MeanMode(arr): 
+    mn =  sum(arr)/len(arr)
+    if arr.count(mn) > 1:
+        return 1
+    return 0
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print MeanMode(raw_input())           
+
diff --git a/easy/23-dash-insert.py b/easy/23-dash-insert.py
index e69de29..15a366a 100644
--- a/easy/23-dash-insert.py
+++ b/easy/23-dash-insert.py
@@ -0,0 +1,29 @@
+def DashInsert(str):
+    ints = list(str)
+    d = list(str)
+    ls = []
+    for el in enumerate(str):
+        idx = el[0]
+        eo = el[1]
+        jt = (idx + 1) % len(str)
+        yt = int(ints[jt])
+        if int(eo) % 2:
+            if yt % 2:
+                ls.append(idx + 1)
+
+    while len(ls) != 0:
+        d[ls[0]:ls[0]] = ['-']
+        ls.remove(ls[0])
+        try:
+            ls[0] += 1
+            if d[(ls[0] - 1)] == '-':
+                ls[0] += 1
+        except:
+            IndexError
+
+    return "".join(d)
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print DashInsert(raw_input())           
+# needs redoing
diff --git a/easy/24-swap-case.py b/easy/24-swap-case.py
index e69de29..eaf931e 100644
--- a/easy/24-swap-case.py
+++ b/easy/24-swap-case.py
@@ -0,0 +1,13 @@
+def SwapCase(str):
+    d = []
+    for letter in enumerate(str):
+        if letter[1].isupper():
+            d.append(letter[1].lower())
+        else:
+            d.append(letter[1].upper())
+
+    return "".join(d)
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print SwapCase(raw_input())           
+
diff --git a/easy/25-number-addition.py b/easy/25-number-addition.py
index e69de29..d3afad3 100644
--- a/easy/25-number-addition.py
+++ b/easy/25-number-addition.py
@@ -0,0 +1,28 @@
+def NumberAddition(str):
+    d = []
+
+    xc = list(str)
+
+    try:
+        while not xc[len(xc) - 1].isdigit() and len(xc) != 0:
+            xc.pop(len(xc) - 1)
+    except:
+        return 0
+
+
+    xs = "".join(xc)
+
+    for ele in xs:
+        if ele.isdigit():
+            d.append(ele)
+        else:
+            d.append('+')
+
+
+    f = "".join(d)
+    return eval(f)
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print NumberAddition(raw_input())           
+
diff --git a/easy/26-third-greatest.py b/easy/26-third-greatest.py
index e69de29..ca0aec3 100644
--- a/easy/26-third-greatest.py
+++ b/easy/26-third-greatest.py
@@ -0,0 +1,12 @@
+def ThirdGreatest(strArr):
+    # code goes here
+    d = []
+    for i,v in enumerate(strArr):
+        d.append((len(v)))
+
+    return "".join(sorted(strArr,key=str.__len__, reverse=True)[2:3])
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print ThirdGreatest(raw_input())           
+
diff --git a/easy/27-powers-of-two.py b/easy/27-powers-of-two.py
index e69de29..7253b05 100644
--- a/easy/27-powers-of-two.py
+++ b/easy/27-powers-of-two.py
@@ -0,0 +1,10 @@
+from math import sqrt
+def PowersofTwo(num):
+  if sqrt(num) % 2 == 0:
+    return 'true'
+  return 'false'
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print PowersofTwo(raw_input())           
+# needs work
diff --git a/easy/28-additive-persistance.py b/easy/28-additive-persistance.py
index e69de29..06fdc30 100644
--- a/easy/28-additive-persistance.py
+++ b/easy/28-additive-persistance.py
@@ -0,0 +1,24 @@
+def AdditivePersistence(num):
+
+    counter = 0
+    ss = num
+
+    while ss >= 10:
+        if ss >= 10:
+            counter += 1
+            ss = mpp(ss)
+    if ss < 10 and num < 10:
+        return 0
+    if ss < 10:
+        return counter
+
+    return counter
+
+def mpp(nm):
+    nm = map(int, str(nm))
+    return sum(nm)
+
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print AdditivePersistence(raw_input())           
+
diff --git a/easy/29-multiplicative-persistance.py b/easy/29-multiplicative-persistance.py
index e69de29..62f27cc 100644
--- a/easy/29-multiplicative-persistance.py
+++ b/easy/29-multiplicative-persistance.py
@@ -0,0 +1,27 @@
+def MultiplicativePersistence(num):
+
+    counter = 0
+    ss = num
+
+    while ss >= 10:
+        if ss >= 10:
+            counter += 1
+            ss = mpp(ss)
+    if ss < 10 and num < 10:
+        return 0
+    if ss < 10:
+        return counter
+
+    return counter
+
+def mpp(nm):
+    nm = map(int, str(nm))
+    p = 1
+    for n in nm:
+        p *= n
+    return p
+    
+# keep this function call here  
+# to see how to enter arguments in Python scroll down
+print MultiplicativePersistence(raw_input())           
+