Making my simple program more efficient [closed] - python-3.x

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I just learned Python two days ago so this is probably very bad. Since i want to get better at optimization and organization, is there anything specifically bad here or that i could improve. Wasted lines of code, things done in a more memory intensive way than it could be and so on. Thanks a lot for any input and I'm looking forward to learn much more.
from random import*
b = 10
a = randint(1,b)
point = 1
x = 1
while x < 2:
print("Guess a number between 1 and ", b)
svar = int (input())
if svar == a:
b+=5
point= point+point
a = randint (1,b)
print("You have ", point, "points!")
elif svar < a:
print("Higher")
else:
print("Lower")

Related

Multiple threads to calculate sums Java [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 4 years ago.
Improve this question
this is how I can find sum of array.
p[i] - array of random integers, size 1000
sum = 0;
for (int j = 1; j < p.length; j++ )
{
sum = sum + p[j];
}
my question is how can I use multiple threads to perform it faster?
Simply:
int sum = Arrays.stream(p).parallel().sum();

Calculating Big O Notation for a algorithm [duplicate]

This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 4 years ago.
I have been asked to explaine about Big O notation and to calculate Big o Notation for an algorithm. I'm done with the defining part but I'm still wondering how I can calculate it. Can someone help me to calculate the Big O for the below given code?
new = int (input("enter number" ))
if new <= 10000:
comm=new*2/100
print (comm)
else :
comm= new*5/100
print (comm)
Since there is no loop, it is O(1).

Arranging people in queue (Uva - 10128) [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
I am trying to solve problem Uva-10128 (Queue) on UVa Online Judge. I am not able find a way to approach this problem. I searched on internet and found that most of people has solved this problem by precalulating using DP.
DP[1][1][1] = 1;
for(N = 2; N <= 13; N++)
for(P = 1; P <= N; P++)
for(R = 1; R <= N; R++)
DP[N][P][R] = DP[N-1][P][R]*(N-2) + DP[N-1][P-1][R] + DP[N-1][P][R-1];
Above code snippet is taken from https://github.com/morris821028/UVa/blob/master/volume101/10128%20-%20Queue.cpp.
Can someone please explain formula used in above code.
Thanks
When you calculate DP[N][P][R] you look at the position of the smallest person in the queue. Because he is the smallest, he can't block anybody. But he will get blocked if he doesn't stand at either end of the queue.
If he is the first person in the queue he is seen from the beginning of the line. So if we remove him, the queue contains N-1 people and you can only see P-1 people from the beginning, but still R people from the end. Therefore there are DP[N-1][P-1][R] combinations.
If he is in the middle, then by removing him we still can see P and R people. And since there are N-2 positions in the middle, there are DP[N-1][P][R] * (N-2) combinations.
And if he is the last person in the queue we get DP[N-1][P][R-1] combinations. The reasoning is identically to the first case.
So the total number of combinations for DP[N][P][R] is the sum of all three cases.

How to multiply input given by user for metric system calculator [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 4 years ago.
Improve this question
I am working on a metric system converter for class using python3 in cs50 but I am having some troubles. Basically, I want the user to input a number(value) and then he'd choose the prefix of measurement(ex: kilo, milli, micro, etc) and when the person does this it multiplies or divides the value by a number to convert it into the requested unit of measurement. For example, if they wanted to convert centimeters to kilometers i want the user to input for example 200 centimeters and then for a function to divide that by 1000 to get 0.002km and to print it to the user. But I have no idea how to go about this. Any help would be appreciated.
I'm not trying to do your homework, but this should give you a first idea:
factor = {'km': 3,
'm': 0,
'dm': -1,
'cm': -2,
'mm': -3} # dictionary with powers, e.g. 1 km = 10**3 m
# For your example of 200 cm = 0.002 km, you type...
number = float(input('numerical value: ')) # 200
unit = input('unit: ') # cm
target_unit = input('target unit: ') # km
print(number * 10**factor[unit] / 10**factor[target_unit], target_unit)
The QuantiPhy package will do this for you.
>>> from quantiphy import Quantity
>>> for v in '1MHz 10ug 1ps'.split():
... q = Quantity(v)
... print(v, q, 1/q, sep=', ')
1MHz, 1 MHz, 1e-06
10ug, 10 ug, 99999.99999999999
1ps, 1 ps, 1000000000000.0
For each case in this example the original string is printed along with the value of the quantity and its reciprocal. This was done to show that quantities are rendered naturally with SI scale factors and units, and that you can use them anywhere a float can be used.

The best way to map correlation matrix from [-1, 1] space to [0, 1] space [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
SO is warning me my question is likely to be closed, I hope they're wrong :)
My question: let you have a correlation matrix; you would like correlations which are next to 1 and -1 go towards 1, while those next to 0 stay there.
The simplest way is to use absolute values, e.g. if Rho is you correlation matrix then you will use abs(Rho).
Is there any way which is theoretically more correct than the one above?
As an example: what if I use Normal p.d.f. instead of absolute value?
Adjusted Rho = N(Rho, mu = 0, sigma = stdev(Rho))
where N is the Normal p.d.f. function.
Have you any better way?
What are strengths and weaknesses of each method?
Thanks,
Try this.
x <- runif(min = -1, max = 1, n = 100)
tr <- (x - min(x))/diff(range(x))
plot(x)
points(tr, col = "red")
You could also use a logit link function that guarantees the values to be between 0 and 1. But given that you're limited to values between -1 and 1, you would get only values in the range of ~[0.3, 1].

Resources