I can only use >, < not min and max functions. The code is supposed to ask for value until the value is a negative number. I know that the first number that comes in is both the largest and the second largest.
Number 2 can then:
be greater than number 1 and then number 2 is the largest and number 1 is the next largest.
be smaller than number 1 and then number 2 is next largest and take 1 largest.
Then comes number 3:
if it is bigger than 1 and 2, number 3 is the biggest
if it is smaller than the largest, say number 1 and larger than the next largest, say number 2, then number 3 becomes the next largest and number 1 the largest
if it is smaller than both, then the largest and the next largest are the same.
How can I use that?
My code:
seclarg = 0
large = 0
while n >= 0 :
n = int(input("Value: "))
if n > large :
large = n
if n > seclarg :
seclarg = n
print(f"Largest: {large}")
print(f"Second largest: {seclarg}")
What I expect to happen is this:
Value: 5
Value: 5
Value: -1
Largest: 5
Second largest: 5
This is what I have now. But an error appears.
large = 0
seclarg = 0
while n > 0 :
n = int(input("Value: "))
if n >= large :
large = n
elif n >= seclarg :
seclarg = n
print(f"Largest: {large}")
print(f"Second largest: {seclarg}")
Related
I am reading a python basics book and there is one function which I don't understand how it works. How is is possible that output looks like pow function even there are not any ** or pow operation? Would be nice if anyone can help because I am getting more and more frustrated
loop while
summary = 1
number = 1
while number <= 6:
i = 1
p = number
while i < 5:
p *= number
i += 1
print(number, "to 5", p)
summary += p
number += 1
print("sum of fifth powers of numbers from 1 to 6 is", summary)
output
1 to 5 1
2 to 5 32
3 to 5 243
4 to 5 1024
5 to 5 3125
6 to 5 7776
sum of fifth powers of numbers from 1 to 6 is 12202
Let me explain this code briefly,
first we are defining,
> summary = 1
> number = 1
Here we are defining and initialising the two variables summary and number.
> while number <= 6:
> i = 1
> p = number
In above code we are starting a while loop which will run while the value of number variable is less than or equal to 6. So, the loop will run from 1 to 6. we are taking a variable i = 1 and p = number here.
> while i < 5:
> p *= number
> i += 1
> print(number, "to 5", p)
> summary += p
> number += 1
> print("sum of fifth powers of numbers from 1 to 6 is", summary)
Now, we are having an another nested while loop and this will run for the values 1 to 4 of i variable. As we can see in the loop, the variable p will be multiplied with itself for 4 times so we will get the 5th power of the particular number. then we are increasing value of number by 1 and adding the value of 5th power in variable summary and lastly we are printing that.
Let me explain with an example
when number=2 (i.e after finding fifth power of 1)
value of p=2 and i=1
then inner loop i.e
while i<5 :
p* = number //i.e p = p*number
i+= 1 //i.e i=i+1
goes like this,
iteration 1: p= 2*2 i.e p=4
i=1+1 i.e i=2 which is less than 5
iteration 2: p= 4*2 i.e p=8
i=2+1 i.e i=3 which is less than 5
iteration 3: p= 8*2 i.e p=16
i=3+1 i.e i=4 which is less than 5
iteration 4: p= 16*2 i.e p=32
i=4+1 i.e i=5 which is equal to 5, so it comes out of loop
therefore, 2 to 5=32
this is how we get fifth power of a number
I wrote this code to find the number with max divisors, but in the case with equal divisors like 672 and 8388608 which both of them have 24 divisors the code cannot select the biggest number and just return the first number with the more number of divisors. In that example, the code returns 672 while it is vivid that 8388608 is much bigger than 672!
please help me to modify my code.
thank you in advance.
def divisors(x):
c = 0
for i in range (1,x+1):
if x % i == 0:
c += 1
return c
m = 0
count = 0
for i in range (20):
a = int(input())
if divisors(a) > count:
m = a
count = divisors(a)
print(m,'',count)
This code will collect the maximum number of divisors in a list, resetting the list if a greater number is found, and appending to the list otherwise. If there are multiple numbers with the same number of divisors, you'll get a list of them:
def divisors(x):
c = 0
for i in range (1,x+1):
if x % i == 0:
c += 1
return c
m = []
count = 0
inputs = 767,665,999,895,907,796,561,914,719,819,555,529,672,933,882,869,801,660,879,985
for a in sorted(inputs):
d = divisors(a)
if d == count:
m.append(a)
elif d > count:
m = [a]
count = d
print(m,'',count)
Output:
[660, 672] 24
If you just want the biggest number, use max(m) in the final print instead.
I Was trying a problem but my time complexity is very high. Is there any way to reduce my time complexity. The Question Below with my code.
A number is said to be a complete ‘n’ digit factor, if it is a factor
of all ‘n’ digit numbers when concatenated (joined to right) to
itself.
For example, 7 is a complete 3-digit factor as it divides all three
digit numbers from 100 to 999 when concatenated to itself (i.e.
100100, 101101,102102, ... ..., 998998, 999999).
Given the value of n and m(Max Value to be checked), write a code to
generate all numbers from 2 to m (both inclusive) that are complete
n-digit factor and print ‘No complete factors’ otherwise. For example,
if n is 3 and m is 15 then print 7, 11, 13
N = int(input()) #No Of Digits
M = int(input()) #Max Value to be Checked
#Im Assuming Max Value will be less than or equal to 9
Nu = '100000000'
NuN = '9'*N
NuM_1 = int(Nu[:N])
NuM_2 = int(NuN)
Lis = [int(str(i)*2) for i in range(NuM_1,NuM_2+1)]
Count = 0
Res = []
for i in range(2,M+1):
Count = 0
for j in Lis:
if(j%i==0):
Count += 1
if(Count==len(Lis)):
Res.append(i)
if(len(Res)!=0):
for i in Res:
print(i)
You're attacking the problem from the functional definition, rather than analyzing the numerical properties of that definition.
Concatenating an n-digit number to itself is the same as multiplying by 10^n + 1. For instance, doing this with 3-digit numbers is equivalent to multiplying each by 10^3 + 1, or 1001.
A number divides all such integers iff that number divides the multiplier. Therefore, you can drop this massive iteration and check; simply factor 10^n + 1.
For instance, 1001 factors into 7 * 11 * 13; from there, you can generate the seven needed integers in the answer: 7, 11, 13, 77, 91, 143, 1001.
There are many available factoring programs; a simple search will find you that code.
Try this variation, using a break, and a faster creation of Lis :
N = int(input("Enter N: ")) #No Of Digits
M = int(input("Enter M: ")) #Max Value to be Checked
#Im Assuming Max Value will be less than or equal to 9
Nu = '100000000'
NuN = '9'*N
NuM_1 = int(Nu[:N])
NuM_2 = int(NuN)
Lis = [i + (i * (10**N)) for i in range(NuM_1, NuM_2+1)]
Res = []
for i in range(2,M+1):
for j in Lis:
if j%i:
break
else:
Res.append(i)
print(Res)
I am trying to get all of the even numbers from the list, using two nested loops and using str(). My code works, just not how I am intending it to and it is short of what I am looking for.
def evnNmbr ():
a = int(input("Enter 1st integer (lower) "))
b = int(input("Enter 2nd integer (higher) "))
evnCnt = 0
for i in range (a, b):
if i % 2 == 0:
evnCnt += 1
s = str(i)
print("Even number count is , ", s, evnCnt)
evnNmbr()
Ex
Enter 1st integer (lower) 0
Enter 2nd integer (higher) 21
Even number count is , 0 1
Even number count is , 2 2
Even number count is , 4 3
Even number count is , 6 4
Even number count is , 8 5
Even number count is , 10 6
Even number count is , 12 7
Even number count is , 14 8
Even number count is , 16 9
Even number count is , 18 10
Even number count is , 20 11
I would like to get the total number of even numbers between the range of numbers a and b. ie: (2, 4, 6, 8 = 4 even numbers
You just need to print the result evn_cnt once at the end
def evnNmbr():
a = int(input("Enter 1st integer (lower) "))
b = int(input("Enter 2nd integer (higher) "))
evn_cnt = 0
for i in range(a, b):
if i % 2 == 0:
evn_cnt += 1
print("Even number count is", evn_cnt)
See
Enter 1st integer (lower) 10
Enter 2nd integer (higher) 88
Even number count is 39
This is going to be your simplest solution
def find_evens(a,b):
l = []
for i in range(a,b):
if i % 2 == 0:
l.append(i)
answer = len(l)
return answer
print(find_evens(1,21))
I added range of b + 1, because the range function excludes the last value, so if you put an even number at the end it wouldn't count. The only thing you have to modify really is to print separately the evnCnt variable, so it only shows the total value.
def evnNmbr ():
a = int(input("Enter 1st integer (lower) "))
b = int(input("Enter 2nd integer (higher) "))
evnCnt = 0
for i in range (a, (b + 1)):
if i % 2 == 0:
evnCnt += 1
# s = str(i)
# print("Even number count is , ", s)
# if you still want to visualize every number separately you can uncomment the lines
print(f"Even number count is: {evnCnt}")
evnNmbr()
The function is to take a positive integer n and divide it by 2 (using integer division) until it reaches 1. The output should be the number of times that value is divided. My code so far is
def keep_halving(x):
i=1
for i in range(x):
if x//2==0:
return i
An example of how the function should run is
>>>keep_halving(4)
2
You could try this:
n = int(input("enter number"))
while n > 1:
n //= 2
print (n)
Input: 128
Output:
enter number128
64
32
16
8
4
2
1
This will continuously divide by two using the while loop and stop when n gets to one.
Edit: I read your question too fast. Here is updated code:
count = 0
n = int(input("enter number"))
while n > 1:
n //= 2
print (n)
count +=1
print (count)
This will add one onto count each time n is divided by two.
Input : 128
Output :
enter number128
64
32
16
8
4
2
1
7