check consecutive number in two lists [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
pseudocode:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
for i in range(len(A)):
for j in range(len(B)):
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
print count
Question: how can I implement when A[4,5,6] == B[4,5,6], then skip to A[6,7,8]==B[6,7,8], instead of A[5,6,7]==B[5,6,7]

You can use a flag variable:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
skip = False #this is a flag variable
for i in range(len(A)):
for j in range(len(B)):
if skip:
skip = False
continue
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
skip = True
print count

Related

How to compare all elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This code in under is will let the user enter the number of elements of list and the user need enter the elements ( User will need type number_of_elements_of_list times) and then count how many positive number, negative number, zero number. The code is like this:
the_number_of_elements_of_the_list = int(input())
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
if number_list == 0:
zero_number_count += 1
elif number_list == 1 or number_list > 1:
positive_number_count += 1
elif number_list < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
The code have a problem: The list can not compare like that. It will be error but i don't know how to compare the elements of list. Can you help me solve this problem?
Firstly, as teambob pointed out, add indent to the for-loop block.
Secondly, as DarkKnight pointed out, put the count variables outside the for-loop.
Thirdly, for each iteration, in order to use that value alone, use number_list[i-1] instead of number_list. (The index is i-1 rather than i because the range in your code starts from 1 rather than 0)
The final code would look like:
the_number_of_elements_of_the_list = int(input())
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
if number_list[i-1] == 0:
zero_number_count += 1
elif number_list[i-1] == 1 or number_list[i-1] > 1:
positive_number_count += 1
elif number_list[i-1] < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)

Count simple numbers in diapasone python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)

Python loop and digits [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would need help to write a program that will load the 5 three-digit numbers.
The program should then print a number that has the highest digit of
hundreds, tens and ones.
Here is my code:
max_num = 0
for number in range(1,6):
a = int(input("Enter five three-digit number: "))
s = a//100
d = (a//10)%10
j = a%10
if(s and d and j) > max_num:
max_num = a
print(max_num)
It only prints the first number.
You need to keep the maximum of each digit:
s = d = j = 0
for _ in range(5):
a = int(input("Enter a three-digit number: "))
s = max(s, a//100)
d = max(d, (a//10)%10)
j = max(j, a%10)
print(s*100+d*10+j)
But you might find it easier to keep them as strings and manipulate individual characters.

Replacing numbers ending in 3 and 7 in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Write a program that generates and prints a list of n elements (n informed by the user) containing the natural numbers (starting with 1) and replacing multiples of 3 by the word 'ping', multiples of 7 by the word 'pong', and multiples of 3 and 7 by the word 'ping-pong'
Here is the code for that
result = []
number = eval(input("Enter a whole number: "))
for index in range(number):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0:
result.append("ping")
elif index % 7 == 0:
result.append("pong")
else:
result.append(index)
print(result) == 0
Now also replaces numbers ending in 3 by the word ‘PING’ and numbers ending in 7 by the word ‘PONG’ this I am not sure how to go about doing.
I tried to make your code do what you want while doing as few modifications to it as possible.
Do NOT use eval. Ever. Bad, bad, bad eval. To cast an string to an int, use int().
Your code was starting at 0 when it was asked that it started at 1, I
changed the range.
To know the last digit, I calculated the number modulo 10, based on the clever comment by #Renuka Deshmukh. Other less clever solutions could have been to check the end of the number casted as a string, with str(index).endswith("7") or str(index)[-1] == "7", for example.
What was your print(result) == 0 trying to do? I removed the ==0.
Here is the resulting code:
result = []
number = int(input("Enter a whole number: "))
for index in range(1,number+1):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0 or index % 10 == 3:
result.append("ping")
elif index % 7 == 0 or index % 10 == 7:
result.append("pong")
else:
result.append(index)
print(result)

Multiplication without using the times or division symbol in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
else:
while int(n2) - 1 != 0:
a = int(n) + int(n)
print("" + str(a))
I need this part of code to times n by n2 without using '*' or '/' and I'm not sure how to change this so that it will work. What do I need to change/add to make this work?
Something like this:
lowest, highest = a, b
if b < a: lowest, highest = b, a
total = 0
for _ in range(lowest):
total += highest
print "a x b = %s" % total
You can use a for loop to add n to ans exactly n2 times:
n = 30
n2 = 2
ans = 0
for i in range(n2):
ans += n
print(ans)
If you need to operate on strings (as in your question), you could use the following example:
n = '30'
n2 = '2'
ans = 0
for i in range(int(n2)):
ans += int(n)
print(str(ans))

Categories