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 8 years ago.
Improve this question
I'm doing my first programming course and I'm stuck already.
I'm trying to make a program that will tell you how many toy's you can make given how many pieces the user has.
To create the toy, you need to have 5 upper pieces and 2 lower pieces.
I've assigned the upper pieces to the letter 'a' and lower pieces to the letter 'b'
This is what I currently have done
print "Welcome to Amanda's Toy Factory"
print "At this factory, you will need to have 5 upper pieces and 2 lower pieces to create a toy"
x = input("How many toys would you like to make?")
print "To create",x,"toys, you will need", x*5, "upper pieces and", x*2, "lower pieces"
a = input("How many upper pieces did you bring?")
b = input("How many lower pieces did you bring?")
So for example, if you input you have 7 upper pieces and 5 lower pieces, it should tell you you are able to create 1 toy and that you would have 2 upper pieces left and 3 lower pieces left.
u, l = input('upper? '), input('lower? ')
nToys = min( int(u)/5, int(l)/2 )
upperLeft = u - nToys*5
lowerLeft = l - nToys*2
okey dokey. I take it you're using an older version of python because of the way you're using print. Here's how I'd do it:
a = input()
b= input("How many lower pieces did you bring?")
upper_left_over = a % 5
lower_left_over = b % 2
upper = int(int(a)/5)
lower = int(int(b)/2)
if upper > lower:
toys = lower
print("You can create", str(toys), "toys")
else:
toys = upper
print("You can create", str(toys), "toys")
print(upper_left_over, lower_left_over)
This is obviously messy. You can clean it up. Hope this helps!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
For example the input is three hundred and twenty six and the output is 326.
I have already tried to write all word of specific numbers like one,two,hundred,... in an array and the same for the integers . So they will have the same index like
word[0] is "one" and integer[0] is 1
But after that nothing works .
I really recommend starting with constructing a grammar. Here is an idea on how it can be done:
T :== t | td | d | x
H :== dh | dh&T
d - digit
x - ten, eleven or twelve
t - twenty - ninety
h - hundred
& - and
T - 1-99
H - 1-999
Note that this may be slightly incorrect because English is not my native language. But it shows how to construct a grammar.
So 326 could then be constructed by H -> dh&T -> dh&td
An expanded grammar to include thousands could look like this:
A :== Ha | HaH
a - thousand
312108 could be constructed with A -> HaH -> dh&Tadh&T -> dh&xadh&d
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 have just wasted my 2 hours only for solving this programming question.if any one knows the trick of doing then please share it.question is given below.
You have given an array A having N integers. Let say G is the product of all elements of A.You have to find the number of distinct prime divisors of G.
Input Format
The first argument given is an Array A, having N integers.
Output Format
Return an Integer, i.e number of distinct prime divisors of
G.
Constraints
1 <= N <= 1e5
1 <= A[i] <= 1e5
For Example
Input:
A = [1, 2, 3, 4]
Output:
2
Explanation:
here G = 1 * 2 * 3 * 4 = 24
and distinct prime divisors of G are [2, 3]
Since this seems to be a homework question, I'll give some pushes in the right direction.
Theoretically, this is a very simple thing to do. Anyone can write code that loops through an array and multiplies its elements. It is also very easy to find pseudocode (or even real code) for factorizing a number into its prime factors.
However, this approach will not work here, since we will be dealing with HUGE numbers. The maximum value of G, given your constraints, is (10⁵)^(10⁵) = 10⁵⁰⁰⁰⁰⁰. This by far exceeds the number of electrons in the observable universe. We cannot factorize such huge numbers.
But luckily we don't need to know the value of G. We are only required to calculate it's prime factors, but we don't need to know the value of G to do so. So instead you will have to factorize the individual numbers in the array. I would recommend something like this code:
factors = set()
for num in A:
f = factorize(num) # Function that returns the set of prime factors in num
factors |= f # Add all elements in f to factors
You were only interested in the distinct prime factors, so using a set will take care of that. Just add everything, and it will automatically throw away any duplicates. factorize(x) is a function you will need to write that takes a number as argument and return the set of prime factors.
Good luck!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to create a function that generates all the elements in odd positions (e.g. 1,1 1,3 ) in a say...5-by-8 matrix?
I assume that by "odd" you mean where both the index for row and column are odd. This means your resulting matrix should be 3 by 4. Anyway, your code is below.
function Anew = yourFunc(A)
%Create a test matrix of dimensions 5x8 of random numbers from 1-10
A = randi(10,5,8);
%Set the values of all elements with either index being an even number equal to zero
for r = 1 : size(A,1)
for c = 1 : size(A,2)
if rem(r,2) == 0 || rem(c,2) == 0
A(r,c) = 0;
else
continue
end
end
end
%Delete rows that contain ALL zeros
for r = 1 : (size(A,1)/2) + 1
if A(r,:) == 0
A(r,:) = [];
end
end
%Delete columns that contain ALL zeros
for c = 1 : (size(A,2)/2) + 1
if A(:,c) == 0
A(:,c) = [];
end
end
%State final answer
Anew = A;
end
Please note that there may be a more efficient code solution, but I have never studied computer science (not my engineering discipline), so I don't know any fancy stuff.
Indexing will stop at the greatest possible value. So indexing x=1:2:4 will generate x = [1 3]. x=1:2:1 will generate x = 1. So now you just need to figure out how many elements are in each matrix row and column.
Pro-Tip: everywhere possible in any code you write, use the length() function for indexing. With the code below, A can be any size matrix. This prevents needing to change your code when you change the matrix you want to analyze.
for row=1:2:length(A(:,1))
for col=1:2:length(A(1,:))
% do some operation on A(row,col)
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Interesting thought question for you guys. Given an array of length n, if I were to pick two random indexes in this array, a and b on average how far apart would they be? As in how many steps would I have to take to walk from a to b. There are no restrictions so there's a chance I pick the same index for both, and there's a chance a and b are at opposite ends of the array.
I've thought about this for a while, my initial idea being they're on average n/2(ish) apart, but I think this hunch is incorrect. An index chosen in the center of the array at most would have to walk n/2 places to find its corresponding second choice, whereas only at the ends of the array would the second choice ever be around n distance away.
Thanks!
After scribbling some grids of possible distances for the first few values of n, I think the exact result is in fact given by:
f(n) = (n² - 1) / 3n
Choosing two places in an array is equivalent to splitting the array up into 3 sections. The average size of each of those sections will be n/3 so the average distance between the two points is also n/3.
Using a monte carlo method in python:
from collections import defaultdict
import random
sample = [abs(random.choice(range(0,10)) - random.choice(range(0,10))) for i in range(0,10000)]
avg = float(sum(sample) / len(sample))
print ("Average: %f" % avg)
freq = defaultdict(int)
for s in sample:
freq[s] += 1
scale = 40.0 / max(freq.values())
for i in range(0,10):
print ("%d : %s" % (i, "#" * int(freq[i] * scale)))
Output:
Average: 3.293700
0 : ######################
1 : ########################################
2 : ####################################
3 : ###############################
4 : ##########################
5 : ######################
6 : #################
7 : #############
8 : #########
9 : ####
So, looks like it's n/3 - but it's not evenly distributed.
There is an easy way to know: for all the couples (a, b), computer their distance. Knowing that all the couples (a, b) have the same probability of appearance, you will just need to do the average of those distances in order to answer your question.
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
Im truggling to find a closed form for the number of k-permutations of a set S of cardinality n.
The combinations should regard ordering, but no repitions.
Example:
|S| = n = 3
S = {a,b,c}
k = 2
{a,b}
{b,a}
{b,c}
{c,b}
{a,c}
{c,a}
Anyone could help me out how to compute the number of viable permutations (and not the permutations itself)?
What I've tried:
I've read through different material and found out, that including repitititions it is
O(n) = n^k
My initial though was, that I need to eliminiate the permutations like
{a,a}
{b,b}
{c,c}
But I struggle finding a closed form for the number of perceivable repititions.
You are looking for the number of k-permutations of a set S of cardinality n.
The formulae is well known : n!/(n-k)!
Pseudo-proof :
for the 1st element, you are able to choose among the n elements of S ;
for the 2nd, only among : n-1, because you don't want doublons ;
...
for the ith, only among : n-(i-1) ;
...
for the kth, only among : n-(k-1) ;
So, finally :
n * (n-1) * ... * (n-i) * ... * (n-k+1)
= n! / (n-k)!