SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.
summer_69([1, 3, 5]) --> 9
summer_69([4, 5, 6, 7, 8, 9]) --> 9
summer_69([2, 1, 6, 9, 11]) --> 14
This is the problem. I have been trying to solve this using list slicing but have been unable to. Note: 9 can only be trailing 6, not before 6.
Here’s what my logic is- I found out the index of 6 and assigned it to a variable and I found the variable of 9 and assigned a variable to it too. The other two conditions are satisfied but the condition with 9 doesn’t get satisfied no matter what I try.
Here is some code from my side. Please excuse my bad programming skills.
def summer_69(arr):
if 6 not in arr:
return sum(arr)
elif 6 and 9 in arr:
i = arr.index(6)
y = arr.index(9)
sxy = sum(arr[i:y])
return sum(arr) - sxy
else:
i = arr.index(6)
return sum(arr[:i])
For an array [4, 5, 6, 7, 8, 9] I am getting the output 18.
Done without slicing.
def summer_69(l):
sign, count = False, 0
for num in l:
if num == 6:
sign = True
if not sign:
count += num
if num == 9:
sign = False
return count
my solution is:
def sum67(nums):
add = True
sum1 =0
for i in range(len(nums)):
if add:
if nums[i] == 6:
add = False
else:
sum1 += nums[i]
else:
if nums[i] == 7:
add= True
return sum1
Related
Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.
Examples
scoresClump([3, 4, 5]) → true
scoresClump([3, 4, 6]) → false
scoresClump([1, 3, 5, 5]) → true
A solution below here seems to work but Translating it into Python looks tricky.
function scoresClump(scores) {
for (let i = 0; i < scores.length - 1; i++) {
if (scores[i + 2] - scores[i] <= 2) {
return true;
}
}
return false;
}
I have tried this in Python and it keeps going out of range or giving the wrong output.
arr=[1,3, 5, 5]
i=0
while i<(len(arr)-1):
if (arr[i+2]-arr[i])<=2 and (arr[i+1]-arr[i])<=2:
print("True")
i=i+1
else:
print("False")
break
You're iterating until i < len(arr)-1, but you're reaching for arr[i+2], which is out of range.
In Python you usually do not need to use an index variable. As you've seen they're a source of errors, and Python offers cleaner syntax without using them. For instance:
# unpythonic
for i in range(len(scores)):
print(scores[i])
# pythonic
for score in scores:
print(score)
zip combines two (or more) things you can iterate over.
scores[2:] means "the scores list, but discarding the first two values".
scores = [1, 2, 3, 4, 5, 6]
for a, b in zip(scores, scores[2:]):
print(a, b)
# 1 3
# 2 4
# 3 6
# zip then ends because it reached the end of scores[2:]
So, you can use zip and list slicing to compare the list with itself without index variables:
def scores_clump(scores):
for a, b in zip(scores, scores[2:]):
if b - a <= 2:
return True
return False
scores_clump([1, 3, 4, 5])
# 4 - 1 is not <= 2
# 5 - 3 is <= 2, return True
scores_clump([1, 3, 6, 7, 9])
# 6 - 1 is not <= 2
# 7 - 3 is not <= 2
# 9 - 6 is not <= 2
# reached end of loop with no match, return False
scores_clump([3, 3, 7, 7, 9])
# 7 - 3 is not <= 2
# 7 - 3 is not <= 2
# 9 - 7 is <= 2, return True
Taking a leaf from Max's answer, you can reduce it further using any:
def scores_clump(scores):
return any(b - a <= 2 for a, b in zip(scores, scores[2:]))
You can use this following function -
def scoresClump(nums):
for x in range(2, len(nums)):
if nums[x] - nums[x-2] <= 2:
return True
return False
if you want a one-liner using any() -
def scoresClump(nums):
return any(nums[x] - nums[x-2] <= 2 for x in range(2, len(nums)))
>>> scoresClump([3, 4, 5])
True
>>> scoresClump([3, 4, 6])
False
>>> scoresClump([3, 4, 5, 5])
True
>>> scoresClump([3, 4])
False
>>> scoresClump([3])
False
>>> scoresClump([3, 3, 7, 7, 9])
True
Your if statement will reach arr[:-1] and then try and check two elements ahead of that, which don't exist, therefore going out of range. Rather than a while loop I would try something like this:
for x in range(0, len(arr)-2)
This is basically a "sliding window" problem where window size=3 and we keep iterating over the array until we find a window where difference between adjacent elements is less than or equal to 2.
Here we can simply check if difference between last and first element of the window is <=2 or not because we are given a sorted array.
def scoresClump(array):
for i in range(2,len(array)):
if array[i]-array[i-2]<=2:
return True
return False
print(scoresClump([1, 3, 5, 5]))
Is it possible to print integers in one line inside the brackets? I would like my output to look like this:
[0 1 1 2 3 5 8 13 21 34]
But due to the way I wrote my code I can add brackets at the end of the list 0 1 1 2 3 5 8 13 21 34[] but not start and end.
My code:
n = int(input("Input a number to create Fibonacci sequence: "))
def fibo(n):
if n <=0:
print("Incorrect input")
return []
if n == 1:
return [0]
a,b = 0,1
for i in range(0,n):
print(a , end = " ",)
#assign a=b and b=a+b to get the sequence
a,b=b,a+b
print()
fibo(n)
The output format you are looking for is the one you get when you print a list.
You can use recursion to progressively build th eresulting list:
def fibo(n,a=0,b=1): return [a]+fibo(n-1,b,a+b) if n else [a]
print(fibo(10)) # fibonacci up to index 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
It can also be done iteratively
n = 10 # first 10 fibonacci numbers
fibo = [0,1]
while len(fibo) < n: fibo.append(sum(fibo[-2:]))
print(fibo[:n])
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
I'm working on a validator of credit cards. That sphere is new for me, so please, don't laugh:D
I'm trying to finish it without any libraries.
def creditCardValidation(creditcard):
creditcard = creditcard.replace( ' ', '' )
creditcard = [int(i) for i in creditcard]
evens = creditcard[::2]
odds = creditcard[1::2]
evens = [element * 2 for element in evens]
for j in evens:
if j >= 10:
j = [int(d) for d in str(j)]
for x in j:
evens.append(x)
for j in evens:
if j >= 10:
evens.remove(j)
return ((sum(evens) + sum(odds)) % 10 == 0)
creditCardValidation('1234 5678 9101 1213')
creditCardValidation('4561 2612 1234 5464')
creditCardValidation('4561 2612 1234 5467')
So the problem is in the array evens.
It returns
[2, 6, 14, 0, 2, 2, 1, 0, 1, 4, 1, 8]
[8, 4, 2, 2, 6, 12, 1, 2, 1, 0, 1, 2]
[8, 4, 2, 2, 6, 12, 1, 2, 1, 0, 1, 2]
It should return the same results except those which greater than 10. Everything works fine. Take a look at the first array, 18 deleted as well as 10, but 14 is not.
Removing while iterating over the array is not the best thing to do and will mostly result in skipping some elements in the array while iterating, so a safer way to do this
for j in evens:
if j >= 10:
evens.remove(j)
is to collect all the elements you want to remove in another list then subtract it from your original if you are using numpy arrrays or removing them one by one, as python lists has no subtraction operation defined to subtract one array from a another
to_remove = []
for j in evens:
if j >= 10:
to_remove.append(j)
for j in to_remove:
events.remove(j)
or you could whitelist instead of blacklisting
small_evens = []
for j in evens:
if j < 10:
small_evens.append(j)
# use small_evens and discard evens array
A couple of issues:
Python has zero indexed arrays, so evens[::2] is actually returning the first, third, etc. digit. Luhn's algo requires even digits (assuming 1 indexing) to be doubled.
You shouldn't modify a list you are iterating over.
You can simplify, removing a lot of the list creations:
def creditCardValidation(creditcard):
*creditcard, checkdigit = creditcard.replace(' ', '')
total = 0
for i, digit in enumerate(map(int, creditcard), 1):
if i % 2 == 0:
digit *= 2
total += digit // 10 # Will be zero for single digits
total += digit % 10
return 9*total % 10 == int(checkdigit)
In []:
creditCardValidation('1234 5678 9101 1213')
Out[]:
True
In []:
creditCardValidation('4561 2612 1234 5464')
Out[]:
False
I am total newbie in python who is practising right now with little 'algorithms'.
So here is my exercise:
N-number integers are given (user have to input). The task is to find out if this sequence is one in which the same numbers are in pairs, but there is one other number between the same numbers.
For example, for a sequence of 11 numbers 3 3 5 10 10 1 12 12 3 6 6, the answer is "yes" and for the sequence 2 2 3 15 15 4 4 8 4 4 1 the answer is "no".
Here's what I tried. But this gypsy-code is not working well:
n = int(input())
right_sequence = True
for i in range(n):
current = int(input())
if i > 0:
if current == previous:
if previous != current:
right_sequence = True
else:
right_sequence = False
previous = current
if right_sequence == True:
print('Yes')
else:
print('No')
I tried to use your own code as much as possible.
Basically the number of inputs should be at least 5. (The shortest correct answer would be in this format : A A B A A which has 5 input.
If we replace the i's with their remainder to 3. (i.e i % 3) Then the indexes for 8 input values would be:
0 1 2 0 1 2 0 1 ...
For which a correct answer would look like bellow:
A A B A A B A A ...
A correct list (The one that outputs "Yes") is the one that:
all the 0 indexes are different from their previous value (Except the first 0 which deosn't have previous value)
all the 1 indexes are equal to their previous value
all the 2 indexes are different from their previous value
The list ends with a 1 index
These 4 points are summarized into 4 if conditions in the bellow code. (The ones that have the value 'k' which carries the remainder of i'th to 3)
n = int(input())
right_sequence = True
k = 0
if n < 5:
right_sequence = False
for i in range(n):
current = int(input())
if i > 0:
k = i % 3
if k == 0:
if current == previous:
right_sequence = False # Print (No)
if k == 1:
if current != previous:
right_sequence = False # Print (No)
if k == 2:
if current == previous:
right_sequence = False # Print (No)
previous = current
if k != 1:
print('No')
elif right_sequence == True:
print('Yes')
elif right_sequence == False:
print('No')
You could slices and zip:
def f(l):
all(a == b != c for a, b, c in zip(x[::3], x[1::3], x[2::3]))
f([3, 3, 5, 10, 10, 1, 12, 12, 3, 6, 6])
# True
f([2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1])
# False
This will work only if the sequence starts with a pair and you might have to check special cases at the end, but it should hint in the right direction.
I have solved the issue the following way:
x = [2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1]
y = [3, 3, 5, 10, 10, 1, 12, 12, 6, 6]
def check_order(x):
for i in range(len(x)):
only_equal_in_row = True # all previous checked j index elements were equal to i
for j in range(i+1, len(x)):
if x[i] == x[j]:
if only_equal_in_row is False: # check if there was j not equal to i elements before
return False
else:
only_equal_in_row = False # found j element not equal to i
return True
if __name__ == "__main__":
print(check_order(x))
print(check_order(y))
Edit: Without functions due to OP request:
x = [2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1]
is_right = True
stop = False # need to stop outer for loop
for i in range(len(x)):
if stop:
break
only_equal_in_row = True # all previous checked j index elements were equal to i
for j in range(i+1, len(x)):
if x[i] == x[j]:
if only_equal_in_row is False: # check if there was j not equal to i elements before
is_right = False
stop = True
break
else:
only_equal_in_row = False # found j element not equal to i
print(is_right)
Suppose we need to transform an array of integers and then compute the sum.
The transformation is the following:
For each integer in the array, subtract the first subsequent integer that is equal or less than its value.
For example, the array:
[6, 1, 3, 4, 6, 2]
becomes
[5, 1, 1, 2, 4, 2]
because
6 > 1 so 6 - 1 = 5
nothing <= to 1 so 1 remains 1
3 > 2 so 3 - 2 = 1
4 > 2 so 4 - 2 = 2
6 > 2 so 6 - 2 = 4
nothing <= to 2 so 2 remains 2
so we sum [5, 1, 1, 2, 4, 2] = 15
I already have the answer below but apparently there is a more optimal method. My answer runs in quadratic time complexity (nested for loop) and I can't figure out how to optimize it.
prices = [6, 1, 3, 4, 6, 2]
results = []
counter = 0
num_prices = len(prices)
for each_item in prices:
flag = True
counter += 1
for each_num in range(counter, num_prices):
if each_item >= prices[each_num] and flag == True:
cost = each_item - prices[each_num]
results.append(cost)
flag = False
if flag == True:
results.append(each_item)
print(sum(results))
Can someone figure out how to answer this question faster than quadratic time complexity? I'm pretty sure this can be done only using 1 for loop but I don't know the data structure to use.
EDIT:
I might be mistaken... I just realized I could have added a break statement after flag = False and that would have saved me from a few unnecessary iterations. I took this question on a quiz and half the test cases said there was a more optimal method. They could have been referring to the break statement so maybe there isn't a faster method than using nested for loop
You can use a stack (implemented using a Python list). The algorithm is linear since each element is compared at most twice (one time with the next element, one time with the next number smaller or equals to it).
def adjusted_total(prices):
stack = []
total_substract = i = 0
n = len(prices)
while i < n:
if not stack or stack[-1] < prices[i]:
stack.append(prices[i])
i += 1
else:
stack.pop()
total_substract += prices[i]
return sum(prices) - total_substract
print(adjusted_total([6, 1, 3, 4, 6, 2]))
Output:
15
a simple way to do it with lists, albeit still quadratic..
p = [6, 1, 3, 4, 6, 2]
out= []
for i,val in zip(range(len(p)),p):
try:
out.append(val - p[[x <= val for x in p[i+1:]].index(True)+(i+1)])
except:
out.append(val)
sum(out) # equals 15
NUMPY APPROACH - honestly don't have alot of programming background so I'm not sure if its linear or not (depending on how the conditional masking works in the background) but still interesting
p = np.array([6, 1, 3, 4, 6, 2])
out = np.array([])
for i,val in zip(range(len(p)),p):
pp = p[i+1:]
try:
new = val - pp[pp<=val][0]
out = np.append(out,new)
except:
out = np.append(out,p[i])
out.sum() #equals 15