Issue when Using Counter to count pairs Python - python-2.7

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

Related

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

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

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.

How to modify the Lua If Statement?

I was trying to use the Lua if function to represent the result with a “*” in front of the value under a circumstance using an if function.
fefec C2
--------------
2 *2
3 3
When the value in Column 1 (fefec) is smaller than 3, then it is shown as * with the value entered in C2. When the value in C1 is equal or greater than 3, it is shown the actual value in C2. The code I wrote is
(function()
if [f#fefec] < 3 then
return "*[f#fefec]"
else return [f#fefec]
end
end)()
But for the first row, I got *(2.000000000000), how can I get rid of these 0s after 2?

Need help making a list for my programming 101 class

im new to programming and my programming assignment wants me to Create a 'list' called my_numbers which contains the odd numbers between 0 and 100 using a for-loop and I am trying to figure out how I can make a list and add 2 to the number in front of it and keep repeating. here is my code so far in python 3.5, thanks in advance to anyone who answers.
my_numbers = [1]
for i in range (0,100):
my_numbers = my_numbers + [2]
So your solution is:
my_numbers = range(1, 100, 2)
1 - start of range, inclusive
100 - end of range, non-inclusive
2 - step
Or in a for loop:
a = []
for x in range(1, 100):
if x % 2 == 1:
a.append(x)
Or in plain words:
For every number between 1 and 100, if you divide that number with 2 and have a reminder of 1 then add that number to the list.

Squeak Smalltalk getting "adaptToNumber:andSend:" error when creating linked list

I have defined list structure and now I am trying to create a list with some rules.
Here is the code so far:
List: aa
| i start current aNumber|
start := Test new setValue:2.
current := start.
aNumber:=3.
i:=0.
[i<=aa] whileTrue:[
current:=start.
[aNumber \\ current =0] whileFalse:
[[current getNext = nil] ifTrue:[current addLinkedVlue:aNumber. i=i+1].current:=current getNext].
aNumber:=aNumber+1].
I have method printListFrom which gets parameter aa. The aim is to get a list which has lenght of aa. The list can't contain numbers which can be divided without reminder by numbers which already are in list. Forexample, if list contains numbers: 2 3 5 and we need to check 6 then 6/2 = 3, it has no reminder, so we can't add it to the list. If we want to check 7, then 7/2=3 reminder 1, 7/3 = 2 reminder 1, 7/5 = 1 reminder 2, in this case we can add 7 to the list.
If we have that aa = 3, as a result I must get list which has 3 numbers(2 3 5), if aa = 4, then list should be (2 3 5 7) and so on.
In my code, in whileTrue loop I divide aNumber by number in the list (current), if I get that reminder is 0 I just add 1 to aNumber, if reminder is greater than 0 then I divide aNumber by next number in list, if I have divided aNumber by all numbers in list and got some reminder after every division I add 1 to i which represents the lenght of the list, and I add also aNumber to the list.
Code is not running well, i am getting :
MessageNotUnderstood: adaptToNumber:andSend:
and I don't know whats wrong.
Here is declaration of other methods which I have declared and used in List method:
setValue: i
a := i.
getNext
^next
addLinkedValue: n
next := Test new setValue: n.
The problem is in this line:
[aNumber \\ current] whileFalse:
because aNumber is an Integer but current isn't. In fact current is an instance of the Test class and therefore aNumber doesn't know how to deal with the message \\ when the argument (current in this case) is not an Integer.
What the receiver aNumber does to try to resolve this problem is to ask the argument to cope with the situation, and to do so it informs the argument to adaptToInteger: aNumber andSend: #quo:.
Note that the selector is not \\ but quo:. The reason is in the way \\ is implemented, which makes the receiver realize that the argument is not an Integer not before sending it the message quo:.
Now, given that you haven't implemented adaptToInteger:andSend: in Test the implementation inherited from Object enters the scene and the instance of Test receives adaptToNumber:andSend:, which is more general.
A solution would then consist in implementing this method in Test as
adaptToNumber: aNumber andSend: aSymbol
^aNumber perform: aSymbol withArgument: a
which delegates the message to the (numeric) instance variable a.
Having said this, I would recommend you to also revise the code trying to make it simpler.