I know there are multiple questions asked reguarding finding the most frequent numbers and how many times they have been repeated. However, I have a problem that requires to sole the question only using for loop, if, etc.
I'm not allowed to use .count, dic, arrary or any other fancy functions.
My_list=[1,1,1,1,1,1,1,1,2,2,2,3,4,5,6,6,7,7,8,7,8,8,8,8,8,8,8]
The answer that is required to print would be
1, 8times 8, 8times
I know it may be a pain to use only for loop, but it's killing me and i'm craving for hlep :(
There are a lot of questions that exist will practice iterative and list. I do not think so this is a good practice. For your pain, I thought to provide you a little bit of a messy answer (messy means a lot of use of variables).
You have not mentioned length of your list. Therefore, I have created this code to work with any range.
Code with comments
My_list=[1,1,1,1,1,1,1,1,2,2,2,3,4,5,6,6,7,7,8,7,8,8,8,8,8,8,8]
list1 = []
list2 = []
list3 = []
c = 0
k = 1
y = 1
while y == 1: # Use while loop until all objects read and store in another lists
for i in My_list: # read oblectsa in My_list one by one
if i == k:
list1.append(k) # append all same digits into list1
list2.append(len(list1)) # Get the length of list1 that have same digits and store it in list2
list3.append(list1[0]) # Get the first value of list1 that have same digits and store it in list2
list1 = [] # Reset the list one for store next same digits
k = k + 1
if k == My_list[-1] + 1: # get the value of last digit of the list
y = 0
m = 0
for j in list2: # use this for loop to get final outcome
print(m, ",", j, "times", list3[m], ",", j, "times")
m = m + 1
Code without comments
My_list=[1,1,1,1,1,1,1,1,2,2,2,3,4,5,6,6,7,7,8,7,8,8,8,8,8,8,8]
list1 = []
list2 = []
list3 = []
c = 0
k = 1
y = 1
while y == 1:
for i in My_list:
if i == k:
list1.append(k)
list2.append(len(list1))
list3.append(list1[0])
list1 = []
k = k + 1
if k == My_list[-1] + 1:
y = 0
m = 0
for j in list2:
print(m, ",", j, "times", list3[m], ",", j, "times")
m = m + 1
Output -:
0 , 8 times 1 , 8 times
1 , 3 times 2 , 3 times
2 , 1 times 3 , 1 times
3 , 1 times 4 , 1 times
4 , 1 times 5 , 1 times
5 , 2 times 6 , 2 times
6 , 3 times 7 , 3 times
7 , 8 times 8 , 8 times
Note -:
You can use print(list2) and print(list3) end of the code to see what happens. And also try to understand the code by deleting part by part.
I am trying to reverse a string number from right to wrong with odd index I have done from left to right but can you some one help how to do it from right to left
for i in range(0, len(number), 2):
print(number[i])
something like
for i in range (-len(length), len(length), -2)
let's say a number 1234567
i want to print 7,5,3,1.
num = '123456789'
for i in num[::-1][::2]:
print(i)
Output -
9
7
5
3
1
You can use the slice notation [start:end:increment] to do
value = "123456789"
for v in value[::-2]:
print(v)
To have
9
7
5
3
1
number = "1234567"
for i in range (len(number)-1,-1,-2):
print (number[i])
output:
7
5
3
1
is that what you want?
Suppose your string is:
number = "123456789"
then,
for i in range(0, len(number), 2):
print(number[len(number) - i - 1])
Update: Now I think I understood correctly:
number = "1234567"
for i in range(len(number), 0, -2):
print(number[i-1])
Write a Python program that reads an integer that gives the number of integers to be read and then reads these integers, one per line, into a list. Print the total of these integers except that if an integer appears more than once it will not be counted.
You may not use the count method of lists. For example, the input:
• 5 1 2 3 4 5 would give 15;
• 5 1 2 3 4 2 would give 8;
• 5 1 2 1 4 2 would give 4; and
• 5 2 2 2 2 2 would give 0.
My code works but is a little hard to read, anyways to so simply this without imports?
xs = [int(input()) for i in range(int(input()))]
print(sum([xs[i] for i in range(len(xs)) \ if xs[i] not in xs[:i] + xs[i + 1:]]))
Split the counting and summing steps. Do one pass over the list to determine the unique elements, then another to sum them.
from collections import Counter
def sum_unique(inputs):
counts = Counter(inputs)
return sum(num for num, count in counts.items() if count == 1)
xs = [int(input()) for i in range(int(input()))]
print(sum_unique(xs))
Edit: Sorry, I didn't see "without imports". You can make a regular dict act like a Counter, it's just not as pretty.
def sum_unique(inputs):
counts = {}
for x in inputs:
counts[x] = counts.get(x, 0) + 1
return sum(num for num, count in counts.items() if count == 1)
I have a 2-D 6x6 array, A.
I want its values to be input by the user in the following format or example:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
where the 0's indicate the places where the user would write their values.
This is my code. It returns an error in split().
def arr_input(x):
for i in range(6):
for j in range(6):
n = int(input().split(' '))
if n>=-9 and n<=9:
x[i][j] = n
print "\n"
I don't want input in a single line. Please help!
EDIT 1
The code I needed was already provided :D. Nevertheless, I learned something new and helpful. Here is the existing code to do the task I wanted:
arr = []
for arr_i in xrange(6):
arr_temp = map(int,raw_input().strip().split(' '))
arr.append(arr_temp)
First of all, you are using input() which returns int when you enter numbers in terminal. You should use raw_input() and get it line by line.
Second, you are trying to convert a list to integer, you should loop through the list values, convert and insert on the resulting list.
Fixed code:
def arr_input(x):
for i in range(6):
num_list = raw_input().split(' ')
for j, str_num in enumerate(num_list):
n = int(str_num)
if n >= -9 and n <= 9:
x[i][j] = n
print "\n"
Here, I used enumerate() to loop though the number list by getting its index each iteration.
There's an inconsistency in how you're treating the input. In python 2.7, the input() function is designed to read one, and only one, argument from stdin.
I'm not exactly sure which way you're trying to read the input in. The nested for loop suggests that you're trying to read the values in one by one, but the split suggests that you're doing it line by line. To cover all bases, I'll explain both cases. At least one of them will be relevant.
Case 1:
Let's say you've been inputting the values one by one, i.e.
1
4
9
4
...
In this case, what's happening is that the input() function is automatically parsing the inputs as integers, and when you try running split() on an integer there's a type error. Python is expecting a string and you're providing an int. That's going to break. There's an easy solution--this can be fixed by simply replacing that line with
n = input()
Case 2: Let's say you're inputing the numbers line by line, as strings. By this, I mean something like:
"1 3 4 5 7 9"
"4 1 8 2 5 1"
...
What's occurring here is that int(...) is trying to cast a list of strings into an integer. That will clearly break the code. A possible solution would be to restructure the code by gett rid of the inner for loop. Something like this should work:
def arr_input(arr):
for i in range(6):
s = input()
nums_s = s.split(' ')
nums = [int(x) for x in nums_s]
arr.append(nums)
print "\n"
return arr
# Usage
a = []
print(a)
a = arr_input(a)
print(a)
Give this one-liner a try:
def arr_input(N=6):
print 'Enter %d by %d array, one row per line (elements separated by blanks)' % (N, N)
return [[n if abs(n)<=9 else 0 for n in map(int, raw_input().split())] for i in range(N)]
The following interactive session demonstrates its usage:
>>> A = arr_input(3)
Enter 3 by 3 array, one row per line (elements separated by blanks)
1 2 -3
4 5 -6
8 9 10
>>> A
[[1, 2, -3], [4, 5, -6], [8, 9, 0]]
I'm taking some guidance from this question (Max path triangle (Python)) but I stumbled upon it after I already started to write out what I thought.
I want to find the maximum of numbers within a triangle, leading from the bottom to the top. So once the loops reach the end the final position of the triangle will be the largest addition of the numbers from the rows below.
For instance...if this was the triangle:
2
3 7
8 2 10
2 6 9 4
It adds row n with row n-1 to mind the maximum values, so if my code ran the triangle would look like this after one iteration.
2
3 7
14 11 19
However the code I've written seems to not replace the elements in the list above.
for i in range(len(a)-1, 0, 1):
for j in range(0, len(a[i])-1, 1):
'''
i = Row position
j = Column position
'''
a[i-1][j] = max(a[i][j] + a[i-1][j], a[i][j+1] + a[i-1][j])
print a
I know it works, because when I put in numbers to check it spits out the correct answer. From the triangle I provided, the first numbers checked would be 2+8 and 6+8, making 14 the correct answer.
So what is wrong with my code?
Thanks :)
In your first for statement, you need to change the delta to -1. You can't go from len(a)-1 to 0 with positive numbers
for i in range(len(a)-1, 0, -1):
for j in range(0, len(a[i])-1, 1):
'''
i = Row position
j = Column position
'''
a[i-1][j] = max(a[i][j] + a[i-1][j], a[i][j+1] + a[i-1][j])
print a