# function that given an array A consisting of N integers, returns the sum of all two -digit numbers. - python-2.7

function that given an array A consisting of N integers, returns the sum of all two -digit numbers.
def solution(A):
# write your code in Python 2.7
sum = 0
for i in A:
if i in range(0,1000):
sum = sum+i
return sum
A = [47,1900,1,90,45]
why would i get 183 instead of 182,please assist
Running solution...
Compilation successful.
Example test: [1, 1000, 80, -91]
WRONG ANSWER (got 81 expected -11)
Example test: [47, 1900, 1, 90, 45]
WRONG ANSWER (got 183 expected 182)
Detected some errors.

I think that in the first case you are just considering positive numbers and single digit numbers, which is in turn the problem for the second case.
test 1) 1+80=81
test 2) 47+1+90+45=183

Related

Issue when Using Counter to count pairs Python

I took an online assessment where I was given the following question :
Input 1 : Integer - Length of the list
Input 2 : Integer List - Consists of different numbers
Output : Integer
Question : Find the total number of pairs possible from the list.
Example : 5, [10,23,2,10,23]
Since, 10 & 23 occurs twice, and 2 occurs only once, there are 2 pairs. So, result should be 2.
So, I did the following & I had one of the test cases failed. The test case wasn’t shown so I’m very confused as to where I went wrong. The code is :
dict=Counter(input2)
pairs=0
count=[]
for i in dict.values() :
count.append(i)
for j in count :
pairs+=j//2
return pairs
Except one test case, all the other 7 seems to satisfy. Please help me out.
You can simply divide the value of each entry of the dict that collections.Counter returns by 2:
from collections import Counter
l = [10,10,10,20,20,20,45,46,45]
print({k: v // 2 for k, v in Counter(l).items()})
This outputs:
{10: 1, 20: 1, 45: 1, 46: 0}
Or if you only want the total number of pairs:
print(sum(v // 2 for v in Counter(l).values()))
This outputs:
3

Python 2.7 - Count number of items in Output

I need to count the number of items in my output.
So for example i created this:
a =1000000
while a >=10:
print a
a=a/2
How would i count how many halving steps were carried out?
Thanks
You have 2 ways: the empiric way and the predictible way.
a =1000000
import math
print("theorical iterations {}".format(int(math.log2(a//10)+0.5)))
counter=0
while a >=10:
counter+=1
a//=2
print("real iterations {}".format(counter))
I get:
theorical iterations 17
real iterations 17
The experimental method just counts the iterations, whereas the predictive method relies on the rounded (to upper bound) result of log2 value of a (which matches the complexity of the algorithm).
(It's rounded to upper bound because if it's more than 16, then you need 17 iterations)
c = 0
a = 1000000
while a >= 10:
print a
a = a / 2
c = c + 1

code debugging: 'take a list of ints between 0-9, return largest number divisible by 3'

I'm trying to understand what is wrong with my current solution.
The problem is as follows:
using python 2.7.6"
You have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. if it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.
input: (int list) l = [3, 1, 4, 1]
output: (int) 4311
input (int list) l = [3 ,1 ,4 ,1 ,5, 9]
output: (int) = 94311
This is my code to tackle the problem:
import itertools
def answer(l):
'#remove the zeros to speed combinatorial analysis:'
zero_count = l.count(0)
for i in range(l.count(0)):
l.pop(l.index(0))
' # to check if a number is divisible by three, check if the sum '
' # of the individual integers that make up the number is divisible '
' # by three. (e.g. 431: 4+3+1 = 8, 8 % 3 != 0, thus 431 % 3 != 0)'
b = len(l)
while b > 0:
combo = itertools.combinations(l, b)
for thing in combo:
'# if number is divisible by 3, reverse sort it and tack on zeros left behind'
if sum(thing) % 3 == 0:
thing = sorted(thing, reverse = True)
max_div_3 = ''
for digit in thing:
max_div_3 += str(digit)
max_div_3 += '0'* zero_count
return int(max_div_3)
b -= 1
return int(0)
I have tested this assignment many times in my own sandbox and it always works.
However when I have submitted it against my instructor, I end up always failing 1 case.. with no explanation of why. I cannot interrogate the instructor's tests, they are blindly pitched against the code.
Does anyone have an idea of a condition under which my code fails to either return the largest integer divisible by 3 or, if none exists, 0?
The list always has at least one number in it.
It turns out that the problem was with the order of itertools.combinations(l, b)
and sorted(thing, reverse = True). The original code was finding the first match of n%3 == 0 but not necessarily the largest match. Performing sort BEFORE itertools.combinations allowed itertools to find the largest n%3 == 0.

Mod of two large numbers in C++

I have a class named LargeNum, which stores large numbers by array such as digit[]. Because int is not large enough to store it.
The base is 10000, so number '9876 8764 7263' is stored like:
digit[4] = {9876, 8764, 7263};
(the base can be changed into 10 or 100, like digit[12] = {9,8,7,6,8,7,6,4,7,2,6,3})
The problem is that I want to overload operator %, so than I can get the remainder of two large numbers. Overloading operator *, - between large numbers is finished by dealing with every digit of the large number. But I really don't how to do so with %. Like:
{1234,7890,1234} % {4567,0023}
Can anyone help me?
The pseudocode should be:
while digits_source > digits_base {
while first_digit_source > first_digit_base {
source -= base << (digits_source - digits_base)
}
second_digit_source += first_digit_source * LargeNum.base
first_digit_source = 0
digits_source--
}
while (source >= base) {
source -= base
}
return source
This should take advantage of your "digits" of the large number.
Edit: For simplicity, I am assuming that a single digit of you array can contain (numerically speaking) two digits. If it cannot, then the code would become quite tricky because you cannot do second_digit_source += first_digit_source * LargeNum.base
Edit:
Regarding an example operation (base = 10000)
{65,0000,0099} % {32,0001}
As 65 is > 32, then proceed to do:
65 - 32 = 33
0 - 1 = -1
Then we have {33, -1, 99} % {32, 1}. Proceed again
33 - 32 = 1
-1 - 1 = -2
We have {1, -2, 99} % {32, 1}. Because 32 > 1, the we join the two first digits of the source and we have {1*1000 - 2, 99} % {32, 1}. Now we can go into the simple while, simply by doing the minus operation. The while does a full comparison of source >= base because we cannot afford to have negative digits. However, during the first part of the algorithm we can because we are guaranteeing that the combination of the two first digits will be positive.

combination a of 6a +9b+20c =n when n is range (0, 100)

I want to write a program sort like solving Diophantine Equation:
That is able to identify any number from 0 to 100 that is exactly with combination of 6a+9b+20C, when a, b, c are all non-negative integer between 0 to 10.
And also I want to write a program that is able to identify any number from 0 to 100 that is not exactly with combination of 6a+9b+20C.
I try with following with problem 2:
for num in range(0, 100):
prime = True
for i in range(3, 20, 3):
if (num%i==0):
prime=False
if prime:
print 'Largest number that cannot be bought in exact quantity', num
I can only get as far as this.
This function will give you a dictionary output which will contain all the numbers that are not the combination and the number which is the combination of your equation :-
def inputNo( a, b, c ):
result = {"isComb":[], "notComb":[]}
for i in range(1,100):
if ((6*a)+(9*b)+(20*c) == i ):
result['isComb'].append(i)
else:
result['notComb'].append(i)
return result