find duplicate count in a list - python

I came up with this logic to count the duplicate
1 take input for list length
2 take input of list
3 search in list for the values from zero to last index increment the counter.
I am getting error can anyone help to fix it, I know my this not accurate way to do this can someone help me out
n = int(input())
l1=[]
for i in range(n):
l1.append(input())
print(l1)
count1=0
count2=0
count3=0
count4=0
for j in range(n):
if 1 in l1[0,n-1]:
count1 =count1+1
elif 2 in l1(0,n-1):
count2=count2+1
elif 3 in l1(0,n-1):
count3= count3+1
elif 4 in l1(0,n-1):
count4=count4+1
print(count1)
input
4
1
1
2
3
4
output should be 2

Simply use set() to remove the duplicates from the original list, then take the length of the original list minus the length of the new set:
s = set(l1)
count = len(l1) - len(s)
I don't think this is the optimal way to do it, but it is the shortest and most intuitive way.

There is a pre built function in list to count the elements
data = [1,2,3,4,1,4]
print("Count of 1 =", data.count(1))
print("Count of 2 =", data.count(2))
print("Count of 3 =", data.count(3))
print("Count of 4 =", data.count(4))
But if the number of duplicate elements is what is expected then count of all the elements and sort only those greater than 1.
Get all number of elements which are duplicate
data = [1,2,3,4,1,4]
print(len([data.count(x) for x in list(set(data)) if data.count(x)>1]))

Related

Operations with integers in lists

I have a task in the course which sounds like this:
Write a program that takes as input a list of numbers in one line. The program should output the sum of its two neighbors for each element of this list. For list elements that are extreme, one of the neighbors is the element located at the opposite end of this list. For example, if the input is the list "1 3 5 6 10", then the output is the list "13 6 9 15 7" (without quotes). If only one number came to the input, you need to display it.
Sample Input 1:
1 3 5 6 10
Sample Output 1:
13 6 9 15 7
Problem occurs when i try to operate with the first number after checking if there is one number. It says 'int' object is not iterable. I tried to edit my code with various constructions with if, while, for. But it is all the same. Would you kindly help me to understand how to operate with integers in the list and add the results of the operations to the new one? Here is the code (result is the supposed output):
user_input = input()
lst = [int(i) for i in user_input.split(' ') if i.isdigit()]
result = []
if len(lst) == 1: #Check if one element
print(lst[0])
if len(lst) > 1:
for lst[0] in range(len(lst)): #For the first index
result += (lst[1] + lst[-1])
Following the code you've pasted, this is the way it should be completed:
user_input = '1 3 5 6 10'
lst = [int(i) for i in user_input.split(' ') if i.isdigit()]
result = []
if len(lst) == 1:
print(lst[0])
elif len(lst) == 2: # What happens if there are 2 elements?
print(lst[1], lst[0])
else:
for index in range(len(lst)): # range(n) returns a generator that yields [0, ..., n - 1]
result.append(lst[index - 1] + lst[(index + 1) % len(lst)])
# result.append(n) adds n to the "result" list
# lst[(index + 1) % len(lst)] we need the modulo (%) because a list of n elements
# won't have an index n, thus you want the sum of elements n - 1 and 0 == n % n

Finding the number of matching letters in two different string at the same indices

I am having trouble finish python code.
overlap('','hello') → 0.
I have managed to get the number back when the length of the strings match but if one of the strings has a smaller length than the other. I keep getting index out of range. Can someone help me finish this.
def overlap(string1,string2):
count = 0
for i in range(len(string1)):
for j in range(len(string2)):
if string1[i] == string2[j]:
count = count + 1
i+=1
else:
i+=1
return count
When running this with a function call. if both strings are equal it gives me the correct number, but if one is smaller or longer then its index out of range.
Thanks
Create one for loop which iterates through min(len(string1), len(string2)) and you would avoid problem when one string is smaller than another, see sample below:
def overlap(string1,string2):
count = 0
for i in range(min(len(string1), len(string2))):
if string1[i] == string2[i]:
count = count + 1
return count
print overlap('summer','winter') #2
print overlap('abcb','dbeb') #2
print overlap('summer','sum') #3
print overlap('','winter') #0
Good Luck!
Replace the nested loops and repeat only for the smaller length.
def overlap(string1, string2):
count=0;
len1= len(string1)
len2= len(string2)
smallLen= len1
if len2<len1:
smallLen= len2
for i in range(smallLen):
if string1[i]== string2[i]:
count+= 1
return count
Thinking about it in order of things that need to be done, you have to first figure out which of the two given strings have the shortest length because that will be your bound for the amount of loop iterations. In Python you can do a conditional assignment like:
maxloop = len(str1) if len(str1) <= len(str2) else len(str2)
You make the condition <= because it doesnt matter which is chosen if they're equal, so just pick the first.
Now that you have the amount of iterations you'll do, you can set up the loop and counter:
count = 0
for i in range(maxloop):
if str1[i] == str2[i]:
count += 1
The single if statement is checking the character at position i in both strings and seeing if they are equal, and if they are, then it'll add one to the overlap counter. Then simply return the count after the loop has terminated.
Try this:
count = 0
if len(string1) < len(string2):
for i in range(len(string1)):
if string1[i] == string2[i]:
count += 1
else:
for i in range(len(string2)):
if string1[i] == string2[i]:
count += 1
return count

find major elements appears more than n/3 times

Working on below algorithm puzzle and debugging below solution works, for a few test cases. My confusion and question is, how could we always guarantee the count for an elements appears more than n/3 times have a positive count? There are another 2n/3 elements which could make it count negative? But I tried and it always work in my samples. If anyone could help to clarify, it will be great.
Here are the problem statement and my code/test cases,
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
def majorityElement(nums):
if not nums:
return []
count1, count2, candidate1, candidate2 = 0, 0, 0, 0
for n in nums:
if n == candidate1:
count1 += 1
elif n == candidate2:
count2 += 1
elif count1 == 0:
candidate1, count1 = n, 1
elif count2 == 0:
candidate2, count2 = n, 1
else:
count1, count2 = count1 - 1, count2 - 1
return [n for n in (candidate1, candidate2) if nums.count(n) > len(nums) // 3]
if __name__ == "__main__":
# print majorityElement([1,2,1,3,1,5,6])
print majorityElement([2,3,1,2,1,3,1,5,5,1,6])
thanks in advance,
Lin
Conceptually, we repeatedly apply a reduction operation to the list that involves deleting three pairwise distinct items. This particular code does reductions online, so that the reduced list so far can be described by two different elements and their corresponding counts (because if there were a third element distinct from the other two, then we could reduce). At the end, we consider at most two elements for occurring more than n/3 times.
The interesting part of the correctness proof is a lemma that, whenever we perform this reduction operation, any element that occurred more n/3 times in the old list occurs more than n'/3 times in the new list, where n is the length of the old list and n' = n-3 is the length of the new list. This ensures by induction that the final list contains all elements occurring more than n/3 times in the initial list (but of course the final list contains only two distinct elements).
The proof of the lemma is that, if an item occurs k times out of n in the old list, then at worst it occurs k-1 times out of n-3 in the new list, and if k/n > 1/3, then
(k-1) n
(k-1)/(n-3) = -------
(n-3) n
k (n-3) + 3 k - n
= -----------------
(n-3) n
(k/n - 1/3)
= k/n + 3 -----------
n-3
> 1/3.

From column of results to rows with same results

I'm really a beginner with python and in classe I'm analyzing a coin toss task. The number of tosses is 1000, the results possible are 1,2. I'm asked to create row with sequences of same results (such as 1 1 1 1 1 1 1 1 and then 2 2 2 2 2,..) and give the length of the longest appearing sequence.
from numpy.random import randint, seed
seed(0)
for n in range(1000):
r = randint(1,3)
print(r)
which gives me a single column reporting the results as follows
1
2
1
1
2
1
1
I can't manage to find the appropriate code to create those rows of sequences of same results.
You are printing the result of each iteration of your for loop.
A simple solution is to create 2 lists, each containing every occurrence of either 1 or 2:
list1 = []
list2 = []
In your for loop, you can use the list.append method to add the value of r to the corresponding list:
for n in range(1000):
r = randint(1, 3)
if r == 1:
list1.append(r)
else:
list2.append(r)
This way, list1 contains every occurrence of the number 1, and list2 contains all the number 2.
You can now print both list, and use len() to get the number of elements in each list.
Try this:
count1 = 0
count2 = 0
for n in range(1000):
r = randint(1, 3)
if r == 1:
count1 += 1
elif r == 2:
count2 += 1
if count1 > count2:
print('1'*count1)
print('2'*count2)
print('Longest sequence is of 1\'s, with %s occurences' %count1)
else:
print('1'*count1)
print('2'*count2)
print('Longest sequence is of 2\'s, with %s occurences' %count2)

Trying to systematically remove elements from a list, but some elements are left out?

I have tried to create a list of whole numbers under 1000 (aka, 0 to 999), then delete the numbers that don't comply with a certain rule [ex. must be divisible by 7 or 3]. After, I have find the sum of all these numbers. Below is the code I wrote to do so.
number_list = []
for x in range(1000):
number_list.append(x)
index = 0
element = number_list[index]
max = 1000
deleted = 0
while index < max - deleted:
element = number_list[index]
if element % 7 != 0 or element % 3 != 0:
number_list.remove(element)
deleted = deleted + 1
index = index + 1
print(sum(number_list))
This runs without any issues, but it does not return the correct sum. The correct sum is 214216, but this gives me 261832 instead, leading me to believe that more than 40 elements that should have been deleted, have not been deleted.
How can I fix this problem?
This is one way to do it using list comprehensions:
number_list = [i for i in range(1000) if not i % 7 != 0 or not i % 3 != 0 ]
print sum(number_list) #Python2
Output:
214216
Using list comprehension should be simpler for this problem. Anyway, I'll try to fix your implementation.
The condition element % 7 != 0 or element % 3 != 0 should be and
instead of or.
If a number is deleted in the loop iteration, index shouldn't move to the next, because the next element is at this index now.
In summary:
if element % 7 != 0 or element % 3 != 0:
number_list.remove(element)
deleted = deleted + 1
index = index + 1
should be:
if element % 7 != 0 and element % 3 != 0:
number_list.remove(element)
deleted = deleted + 1
else:
index = index + 1
When you remove elements from the list: number_list.remove(element), the meaning of the index changes so that on the next iteration, after incrementing index, element = number_list[index] now references a value other than the index-th item of the original number_list. Thus, some values in the original number_list are never checked.
The solution, in general, is to not mutate a list while iterating over it. Instead, use a list comprehension (or, for memory-efficiency, a generator expression) to build a new list:
In [107]: sum([i for i in range(1000) if (i % 7 == 0) or (i % 3 == 0)])
Out[107]: 214216
or, iterate over index backwards, counting from 999 down to 0, so that removing the index-th item will not affect the indexing of other items when using a smaller index.
Everything is much simpler.
sum=0
for x in range(1000):
if x % 7 == 0 or x % 3 == 0:
sum=sum+x
print sum
Result:
214216

Categories

Resources