Python storing value of old assigned variable - logic

lets say i have this code
x = 5
while True:
x = 10
break
is there a way I can keep the original value of x and the new assigned value

This pattern might be what you're looking for
x = 5
y = 0
while True:
# y stores old value of x
y = x
x = 10
# example where x actually changes
while True:
y = x
x += 1
print(y, x)

Related

Unable to understand the result of this hash function

I was reading my notes from the algorithms class (several years old) and I found this:
which says: Assuming that
h(k) = k mod m, where m = 4 and k = 100, then h(k) = 4
Is this true? I would think that 4 * 25 = 100, thus h(k) = 0. What am I missing?
I thought it was a typo, but I just checked the newest version of the notes and it's still the same!
The modulo operator can never return that result, as it represents the remainder after integer division.
So this rule holds for positive integers x and y:
x mod y = z ⇒ z < y
Another way to write the above modulo operation is:
⎣x/y⎦.y + z = x
If somehow you would achieve that z == y then obviously you did something wrong in the ⎣x/y⎦ part.

Can someone help me with Pseudocode?

The variable X starts with the value 0.
The variable Y starts with the value 5.
Add 1 to X.
Add 1 to Y.
Add X and Y, and store the result in Y.
Display the value in Y on the screen.
Output #1:
Pseudocode #1:
The logic of the question makes sense, but I do not even know where to start. Can someone explain and/or help me?
Pseudocode:
X = 0;
Y = 5;
X = X + 1;
Y = Y + 1;
Y = X + Y;
display(Y);
I'll leave the maths to you...

factorial algorithm in pseudo code

I've been given the following algorithm, that takes a positive integer K and returns a value:
X = 1
Y = 1
while X ≠ K do
X = X + 1
Y = Y * x
return Y
I'm supposed to figure out what it returns.
As it happens, I know the answer — it returns the factorial of K — but I don't understand why.
How do you go about figuring out what this pseudocode does?
X = 1 <- this is counter which you gonna multiply in every step
Y = 1 <- this is to store the cumulative product after each step
while X ≠ K do <- until you reach K
X = X + 1 <- increase X
Y = Y * X <- multiply with increased X
return Y <- return the product
So in the loop the cumulative product goes like this 1 -> 1*2 - > 2*3 -> 6*4 -> ... -> 1*2*..*(K-1)*K which is K!
Now let's assume that K is 5. Therefore the factorial of 5 is 120.
Then as you enter the loop X value is 2 and y gets the value 2.(1*2)
Then the value of X is 3 after getting into loop, which then makes the value of Y 6 because (3*2).
Then the value of X is 4 after getting into loop, which then makes the value of Y 24 because (4*6).
Then the value of X is 5 after getting into loop, which then makes the value of Y 120.
Then since X==Y the while loop exits and Y value which is the factorial is returned.
this piece of code can be simply rewritten as (in C/C++/Java),
for(X=1;X<=K;X++){
Y=Y*X;
}
Now it describes it self :-)

How can I use user inputs for math?

I have this function:
def func()
puts "Give a value for x \n>"
x = gets.chomp
puts "Give a value for y \n>"
y = gets.chomp
z = x + y
puts z
end
If a user inputs 5 for x and 5 for y, I want z to make 5 + 5 and print 10, but this will print 55 instead.
The values you've read are stored as strings, and with strings, the + operator performs concatenation. You need to convert both inputs to integers if you want to perform integer arithmetic:
z = x.to_i + y.to_i
Another way to get there:
x = '5'
y = '5'
[x, y].map(&:to_i).inject(:+)
=> 10
z = x.to_i + y.to_i
Just convert input strings to integer or float (to_f)

How do you build a ratings implementation?

We have need for a "rating" system in a project we are working on, similar to the one in SO. However, in ours there are multiple entities that need to be "tagged" with a vote up (only up, never down, like an increment). Sometimes we will need to show all of the entities in order of what is rated highest, regardless of entity type, basically mixing the result sets, I guess. What data structures / algorithms do you use to implement this so that is flexible and still scalable?
Since reddit's ranking algorithm rocks, it makes very much sense to have a look at it, if not copy it:
Given the time the entry was posted A and the time of 7:46:43 a.m. December 8, 2005 B we have ts as their difference in seconds:
ts = A - B
and x as the difference between the number of up votes U and the number of down votes D:
x = U - D
Where
y = 1 if x > 0
y = 0 if x = 0
y = -1 if x < 0
and z as the maximal value of the absolute value of x and 1:
z = |x| if |x| >= 1
z = 1 if |x| < 1
we have the rating as a function ƒ(ts, y, z):
ƒ(ts, y, z) = log10 z + (y • ts)/45000

Resources