Okay, so I'm trying to write a simple program that gives me both the positive
and negative range of the number given by the user.
For example, if the user gives number 3 then the program should prints out
-3 -2 -1 0 1 2 3
I've tried thinking but just can't think how to get the negative range outputs.
But the code I got below only gives me the positive range outputs, so I was
thinking may I need to do to make it gives me both the positive and negative
outputs.
s = int(input("Enter a number "))
for i in range(s+1):
print i+1
Range() can take two parameters: range(start, end), where start is inclusive, and end is exclusive. So the range you want for 3 is: range(-3, 4). To make it general:
s = int(input("Enter a number "))
for i in range(-s, s+1):
print i
Related
I was doing some ds and algo problems from faceprep and a question came:
Amy Farrah Fowler asks her students to give 3 examples for positive odd numbers. When the student specifies a correct answer, his/her score is incremented by 1. When the student specifies a positive even number, his/her score is decremented by 0.5. When the student specifies a negative number, he/she will not be given any more chances to correct his or her mistake and his/her score will be decremented by 1. So a student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number. Some students didn't know the difference between odd numbers and even numbers and they made many mistakes and so it was difficult for her to maintain the scores. She asks for your help. Can you please help her by writing a program to calculate the score?
INPUT & OUTPUT FORMAT:
Input consists of a list of integers.
The output consists of a single line. The score needs to be corrected to 1 decimal place.
[For this exercise, don't worry about duplicate odd numbers. Even if the students specify the same odd number thrice, it is accepted].
input is given as:
1
2
3
4
5
Now How to take input and store it in a variable by single command?
till now I was doing but now number of inputs are different for each case study.
:
a=int(input())
b=int(input())
c=int(input())
I want to do this using a single command so that each input will store in a list.
Try this code snippet to store the input in the list "List_w".
List_w = []
try:
while(1):
List_w.append(int(input()))
except:
print(List_w)
if your input is separated by white space, you can use:
arr = list(map(int, input().split()))
print(arr) # input 1 2 3 4 # output [1,2,3,4]
if your input is based on given range use:
for _ in range(int(input())):
arr = list(map(int, input().split()))
print(arr)
if the inputs separated by new line you can use append method or one liner:
arr = [int(input()) for _ in range(int(input()))]
print(arr)
# input
3
4
5
6 # output [4, 5, 6]
know more about input here https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/
We have to check if there exists an integer A such that it has exactly X positive integer divisors and exactly K of them are prime numbers
This is my code but it is showing wrong answer after submitting
In codeChef you need to take input in stdin format. Try replacing map(int, input.split()) by
import sys
map(int, sys.stdin.split())
because your code is wrong in case of 5 2
r = 4
then x>4
your code shows ans 1
but real ans is 0
I want to add 1 to the very end of a float, like this:
0.0735 + 0.0001 = 0.0736
But I'll get different lengths of floats, and for each I want to add 1 to the very end, like this:
0.000398 would have to be added to 0.000399 and 0.000000281 would have to be 0.000000282.
Also, if it's something like this: 0.0000280, I need to add it to the 0, not the 8. Which makes me believe that I have basically one problem: I need to find the first number after 0 and then count 2 more whatever it is, and add 1 to it (basically keeping 3 numbers, even if there's a 0 after)
Is there a way to do that?
edit: I tried this already len(str(n)) but the problem is the zeros, as I don't have control over the length of the number, a number like this: 0.01 should be added to 0.0101 but if I have 0.0111 I want 0.0112. What I mean is, the program usually hides the zeroes, that's why I said I'd probably need to find the first non zero digit on it.
10^floor(log10(n)) will get you a 1 digit at the same position as the first digit (e.g. 0.0001 for 0.000399), so add 0.01 times that:
n = 0.0735
n += 0.01 * 10 ** math.floor(math.log10(n))
I want to write a regular expression for Binary Numbers Divisible by 5.
I have already done the regular expressions for Binary Numbers Divisible by 2 and for 3 but I couldn't find one for 5.
Any suggestions?
(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$ to test it with regexp. See it working here.
You can build a DFA and convert it to regular expression. The DFA was already built in another answer. You can read it, it is very well explained.
The general idea is to remove nodes, adding edges.
Becomes:
Using this transformation and the DFA from the answer I linked, here are the steps to get the regular expression:
(EDIT: Note that the labels "Q3" and "Q4" have been mistakenly swapped in the diagram. These states represent the remainder after modulus 5.)
2^0 = 1 = 1 mod 5
2^1 = 2 = 2 mod 5
2^2 = 4 = -1 mod 5
2^3 = 8 = -2 mod 5
2^4 = 16 = 1 mod 5
2^5 = 32 = 2 mod 5
... -1 mod 5
... -2 mod 5
So we have a 1, 2, -1, -2 pattern. There are two subpatterns where only the sign of the number alternates: Let n is the digit number and the number of the least significant digit is 0; odd pattern is
(-1)^(n)
and even pattern is
2x((-1)^(n))
So, how to use this?
Let the original number be 100011, divide the numbers digits into two parts, even and odd. Sum each parts digits separately. Multiply sum of the odd digits by 2. Now, if the result is divisible by sum of the even digits, then the original number is divisible by 5, else it is not divisible. Example:
100011
1_0_1_ 1+0+1 = 2
_0_0_1 0+0+1 = 1; 1x2 = 2
2 mod(2) equals 0? Yes. Therefore, original number is divisible.
How to apply it within a regex? Using callout functions within a regex it can be applied. Callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching.
However, ndn's answer is more appropriate and easier, therefore I recommend to use his answer.
However, "^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))1)$" matches empty string.
I'd like to take raw input from a user and then re-arranging the output in a different order. I have the code working, but the output has single quotes around each number. Here is the really simple code:
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(CodeNumber[3], CodeNumber[1], CodeNumber[0])
print "Code_B: %r%r%r" %(CodeNumber[2], CodeNumber[3], CodeNumber[1])
The output I get is:
Please enter 4 digit number: 1234
Code_A: '4''2''1'
Code_B: '3''4''2'
How do I remove the single quotation marks so I get 421 instead of '4''2''1'?
At least one way to do this is to use int() on element indices of CodeNumber to turn the strings into numbers.
This is your edited code (by me, hob!)
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(int(CodeNumber[3]), int(CodeNumber[1]), int(CodeNumber[0]))
print "Code_A: %r%r%r" %(int(CodeNumber[2]), int(CodeNumber[3]), int(CodeNumber[1]))
Output:
Please enter 4 digit number: 1234
Code_A: 421
Code_B: 342
Running time = O(n)