So I need to write a program which generates random numbers from 100 to 999, and the tricky part is that the digits in the number can't be the same.
For example: 222, 212 and so on are not allowed.
So far, I have this:
import random
a = int (random.randint(1,9)) <-- first digit
b = int (random.randint(0,9)) <-- second digit
c = int (random.randint(0,9)) <-- third digit
if (a != b and a != b and b != c):
print a,b,c
As you can see, I generate all three digits separately. I think it's easier to check if there are same digits in the number.
So, now I want to do a loop that will generate the numbers until the requirements are met (now it either prints blank page or the number). Note that it generates only once and I have to open the program again.
In C++ I did that with the 'while loop'. And here I don't know how to do it.
And one more question. How can I code the number(quantity) of random numbers I want to generate?
To be more specific:
I want to generate 4 random numbers. How should I code it?
P.S.
Thank you all for your answers and suggestions, I am really keen on learning new techniques and codes.
It might be easier to use random.sample to sample from the distribution [0,9] without replacement, rejecting any samples which select 0 first:
import random
def pick_number():
a = 0
while a == 0:
a, b, c = random.sample(range(10), 3)
return 100*a + 10*b + c
Further explanation: range(10) (in Python 2) generates the list [0,1,2,3,4,5,6,7,8,9], and random.sample picks 3 elements from it without replacement. The while loop repeats until the first element, a is not zero, so when it exits you have the three digits of a number that meets the requirements. You can turn this into a single integer by multiplying the first by 100, the second by 10 and then adding all three.
To loop until it's ok you can also use while in Python:
from random import randint
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
You can also put it in a function:
def generate_number():
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
return (a, b, c)
And if you want n such numbers (they are not actually numbers since (a, b, c) is a tuple of 3 int values), you can call it n times:
for i in range(n):
print(generate_number())
If you prefer formatting the values, you can also do:
for i in range(n):
print('%d %d %d'%generate_number()) # old style formatting
print('{} {} {}'.format(*generate_number())) # new style
Finally, you can use get n from the command line:
import sys
n = sys.argv[1]
Or you can ask it directly:
n = int(input("Please enter some number: ")) # in python2.x you'd use raw_input instead of input
You'll get an exception if the value cannot be converted; you can catch the exception and loop as for the generation of numbers.
Putting it all together with the typical main construct:
from random import randint
import sys
def generate_number():
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
return (a, b, c)
def main():
n = sys.argv[1]
for i in range(n):
print('{} {} {}'.format(*generate_number()))
if __name__=='__main__':
main()
Instead of generating 3 random numbers create a list of numbers 0-9 then call random.shuffle on it. You then just pull out as many digits as you need from the shuffled list and no digit will be repeated
import random
def generate_number():
numbers = range(10) # Generates a list 0-9
random.shuffle(numbers) # Shuffle the list
return (numbers[0], numbers[1], numbers[2]) #take the first 3 items
(This answer is pretty much the same as xnx's answer execpt it uses the method shuffle which can be used in version prior to 2.3 and it does not loop waiting for a non 0 first digit.)
On top of #xnx answer, some background: this is called Fisher-Yates (or F-Y-Knuth) shuffle, and it is similar to randomly picking lottery tickets, using ticket only once. O(n) complexity, more to read here http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
Related
Is there an equation I can use for arbitrary M and N?
Example, N=3 and M=2:
3 bits allow for 8 different combinations, but only 2 of them do not contain more than 2 same symbols in a row
000 - Fails
001 - Fails
010 - OK
011 - Fails
100 - Fails
101 - OK
110 - Fails
111 - Fails
One way to frame the problem is as follows: we would like to count binary words of length n without runs of length m or larger. Let g(n, m) denote the number of such words. In the example, n = 3 and m = 2.
If n < m, every binary word works, and we get g(n, m) = 2^n words in total.
When n >= m, we can choose to start with 1, 2, ... m-1 repeated values,
followed by g(n-1, m), g(n-2, m), ... g(n-m+1, m) choices respectively. Combined, we get the following recursion (in Python):
from functools import lru_cache
#lru_cache(None) # memoization
def g(n, m):
if n < m:
return 2 ** n
else:
return sum(g(n-j, m) for j in range(1, m))
To test for correctness, we can compute the number of such binary sequences directly:
from itertools import product, groupby
def brute_force(n, k):
# generate all binary sequences of length n
products = product([0,1], repeat=n)
count = 0
for prod in products:
has_run = False
# group consecutive digits
for _, gp in groupby(prod):
gp_size = sum(1 for _ in gp)
if gp_size >= k:
# there are k or more consecutive digits in a row
has_run = True
break
if not has_run:
count += 1
return count
assert 2 == g(3, 2) == brute_force(3, 2)
assert 927936 == g(20, 7) == brute_force(20, 7)
I have a list of vars with different values
a = 2
b = 1
c= 12343243
d = 8998
Can find the smallest value
aSmallestVALUE = min([a, b, c, d])
and index
[v,idx]=min([a, b, c, d])
I want to find the index of variable and sort this list from 0 to up
something like the
sorted list = b, a, d, c
Obviously if you want to treat those four variables as a 'list' to be sorted, you need to be working with a 'list' construct, not 4 isolated variables.
L = [2, 1, 12343243, 8998];
Otherwise it makes no sense to talk about the 'index' of an existing independent variable (though obviously you can construct this L from a bunch of pre-existing variables if desired).
With L in hand, you can now do
[minval, idx] = min( L )
% minval = 1
% idx = 2
to find the minimum and its corresponding index, and
[sorted, sortedindices] = sort( L )
% sorted =
% 1.0000e+00 2.0000e+00 8.9980e+03 1.2343e+07
%
% sortedindices =
% 2 1 4 3
to obtain a sorted array, with corresponding indices.
How can I make a function with a vector as input and a matrix as an output?
I have to write a function that will convert cubic meters to liters and English gallons. The input should be a vector containing volume values in m ^ 3 to be converted. The result should be a matrix in which the first column contains the result in m ^ 3, the second liter, the third English gallon.
I tried this:
function [liter, gallon] = function1 (x=[a, b, c, d]);
liter= a-10+d-c;
gallon= b+15+c;
endfunction
You're almost there.
The x=[a,b,c,d] part is superfluous, your argument should be just x.
function [liter, gallon] = function1 (x);
a = x(1); b = x(2); c = x(3); d = x(4);
liter = a - 10 + d - c;
gallon = b + 15 + c;
endfunction
If you want your code to be safe and guard against improper inputs, you can perform such checks manually inside the function, e.g.
assert( nargin < 1 || nargin > 4, "Wrong number of inputs supplied");
The syntax x=[a,b,c,d] does not apply to octave; this is reserved for setting up default arguments, in which case a, b, c, and d should be given specific values that you'd want as the defaults. if you had said something like x = [1,2,3,4], then this would be fine, and it would mean that if you called the function without an argument, it would set x up to this default value.
I am trying to improve my programming skills by writing functions in multiple ways, this teaches me new ways of writing code but also understanding other people's style of writing code. Below is a function that calculates the sum of all even numbers in a fibonacci sequence up to the max value. Do you have any recommendations on writing this algorithm differently, maybe more compactly or more pythonic?
def calcFibonacciSumOfEvenOnly():
MAX_VALUE = 4000000
sumOfEven = 0
prev = 1
curr = 2
while curr <= MAX_VALUE:
if curr % 2 == 0:
sumOfEven += curr
temp = curr
curr += prev
prev = temp
return sumOfEven
I do not want to write this function recursively since I know it takes up a lot of memory even though it is quite simple to write.
You can use a generator to produce even numbers of a fibonacci sequence up to the given max value, and then obtain the sum of the generated numbers:
def even_fibs_up_to(m):
a, b = 0, 1
while a <= m:
if a % 2 == 0:
yield a
a, b = b, a + b
So that:
print(sum(even_fibs_up_to(50)))
would output: 44 (0 + 2 + 8 + 34 = 44)
I wrote this mips code to find the gcf but I am confused on getting the number of instructions executed for this code. I need to find a linear function as a function of number of times the remainder must be calculated before an answer. i tried running this code using Single step with Qtspim but not sure on how to proceed.
gcf:
addiu $sp,$sp,-4 # adjust the stack for an item
sw $ra,0($sp) # save return address
rem $t4,$a0,$a1 # r = a % b
beq $t4,$zero,L1 # if(r==0) go to L1
add $a0,$zero,$a1 # a = b
add $a1,$zero,$t4 # b = r
jr gcf
L1:
add $v0,$zero,$a1 # return b
addiu $sp,$sp,4 # pop 2 items
jr $ra # return to caller
There is absolutely nothing new to show here, the algorithm you just implemented is the Euclidean algorithm and it is well known in the literature1.
I will nonetheless write an informal analysis here as link only questions are evil.
First lets rewrite the code in an high level formulation:
unsigned int gcd(unsigned int a, unsigned int b)
{
if (a % b == 0)
return b;
return gcd(b, a % b);
}
The choice of unsigned int vs int was dicated by the MIPS ISA that makes rem undefined for negative operands.
Out goal is to find a function T(a, b) that gives the number of step the algorithm requires to compute the GDC of a and b.
Since a direct approach leads to nothing, we try by inverting the problem.
What pairs (a, b) makes T(a, b) = 1, in other words what pairs make gcd(a, b) terminates in one step?
We clearly must have that a % b = 0, which means that a must be a multiple of b.
There are actually an (countable) infinite number of pairs, we can limit our selves to pairs with the smallest, a and b2.
To recap, to have T(a, b) = 1 we need a = nb and we pick the pair (a, b) = (1, 1).
Now, given a pair (c, d) that requires N steps, how do we find a new pair (a, b) such that T(a, b) = T(c, d) + 1?
Since gcd(a, b) must take one step further then gcd(c, d) and since starting from gcd(a, b) the next step is gcd(b, a % b) we must have:
c = b => b = c
d = a % b => d = a % c => a = c + d
The step d = a % c => a = c + d comes from the minimality of a, we need the smallest a that when divided by c gives d, so we can take a = c + d since (c + d) % c = c % c d % c = 0 + d = d.
For d % c = d to be true we need that d < c.
Our base pair was (1, 1) which doesn't satisfy this hypothesis, luckily we can take (2, 1) as the base pair (convince your self that T(2, 1) = 1).
Then we have:
gcd(3, 2) = gcd(2, 1) = 1
T(3, 2) = 1 + T(2, 1) = 1 + 1 = 2
gcd(5, 3) = gcd(3, 2) = 1
T(5, 3) = 1 + T(3, 2) = 1 + 2 = 3
gcd(8, 5) = gcd(5, 3) = 1
T(8, 5) = 1 + T(5, 3) = 1 + 3 = 4
...
If we look at the pair (2, 1), (3, 2), (5, 3), (8, 5), ... we see that the n-th pair (starting from 1) is made by the number (Fn+1, Fn).
Where Fn is the n-th Fibonacci number.
We than have:
T(Fn+1, Fn) = n
Regarding Fibonacci number we know that Fn ∝ φn.
We are now going to use all the trickery of asymptotic analysis, particularly in the limit of the big-O notation considering φn or φn + 1 is the same.
Also we won't use the big-O symbol explicitly, we rather assume that each equality is true in the limit. This is an abuse, but makes the analysis more compact.
We can assume without loss of generality that N is an upper bound for both number in the pair and that it is proportional to φn.
We have N ∝ φn that gives logφ N = n, this ca be rewritten as log(N)/log(φ) = n (where logs are in base 10 and log(φ) can be taken to be 1/5).
Thus we finally have 5logN = n or written in reverse order
n = 5 logN
Where n is the number of step taken by gcd(a, b) where 0 < b < a < N.
We can further show that if a = ng and b = mg with n, m coprimes, than T(a, b) = T(n, m) thus the restriction of taking the minimal pairs is not bounding.
1 In the eventuality that you rediscovered such algorithm, I strongly advice against continue with reading this answer. You surely have a sharp mind that would benefit the most from a challenge than from an answer.
2 We'll later see that this won't give rise to a loss of generality.