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
A friend of mine writes poems and he has a very special rhythmic schema behind it. All his poems have 4 staves and each stave has 4 lines. So the rhyme schema is now this:
1
2
3
4
2
5
4
6
5
7
6
8
7
1
8
3
He asked me how many permutations there are and if I could calculate them all, but I don't really know where to start except brute force which, i guess, is not the optimal solution.
(Programming language preferably java(script)/pseudo)
Cheers, Daniel
As each rhyme type occurs exactly twice and 2! = 2 you have 2^8 = 256 possibilities assuming the lines are unequal
As there are 8 kinds of line 1 2 3 4 5 6 7 8
number of kinds the 1st stave can be is 8*7*6*5 = 1680
2nd stave can be 1*4*1*3 = 12
3rd stave can be 1*2*1*1 = 2
and 4th save can be 1*1*1*1 = 1
So the total number of possibility is 1680*12*2*1 = 40320
Consider what it takes to create a poem-permutation, paying close attention to the choices we have to make along the way. First, we make 8 categories, with two rhyming lines (or words) in each:
rhymes = {
'A': ['fade', 'made'],
'B': ['cow', 'how'],
'C': ['can', 'fan'],
'D': ['a', 'hey'],
'E': ['answer', 'hampster'],
'F': ['whiz', 'is'],
'G': ['smut', 'what'],
'H': ['key', 'we'],
}
To create a poem, we need to pick an ordering of the categories. Given the rhyming scheme [1,2,3,4,2,5,4,6,5,7,6,8,7,1,8,3], that could be ABCDBEDFEGFHGAHC. But it could equally well be HGFEGDECDBCABHAF. There are many possible orderings for the categories which fit your rhyming scheme. In total there are 8! = 8*7*6*5*4*3*2*1 = 40320 orderings for the categories. In combinatorics this is called the number of permutations of 8 items.
Now once we have an ordering, such as ABCDBEDFEGFHGAHC, we can construct a poem by selecting 1 of the 2 possible items from category A, then 1 of the 2 possible items from category B, and so on.
How many ways are there to do this? Well there are 2^8 = 256 ways to make 8 independent binary choices. Even though there are 16 lines, after you make the first 8 choices, the rest of the "choices" are forced since there is only one choice left for each category.
So in total there are
8! * 2**8 = 40320 * 256 = 10321920
or a little over 10 million poem-permutations possible.
In Python, which is somewhat close to pseudocode, you could enumerate the poems like this:
import itertools as IT
rhymes = [
['fade', 'made'],
['cow', 'how'],
['can', 'fan'],
['a', 'hey'],
['answer', 'hampster'],
['whiz', 'is'],
['smut', 'what'],
['key', 'we'],
]
scheme = [1,2,3,4,2,5,4,6,5,7,6,8,7,1,8,3]
# shift by 1 since Python uses 0-based indexing
scheme = [i-1 for i in scheme]
# 40320 itmes in orderings
orderings = IT.permutations(rhymes)
count = 0
for ordering in orderings:
# 256 ways to select the lines given an ordering
for lines in IT.product(*[IT.permutations(line)
for line in ordering]):
lines = map(iter, lines)
for i in scheme:
print(next(lines[i]))
count += 1
print
print(count)
which yields, for example,
fade
cow
can
a
how
answer
hey
whiz
hampster
smut
is
key
what
made
we
fan
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 2 years ago.
Improve this question
I would appreciate any help on this problem that I have:
User enters a numeric value, like 6.
Now program has to fill out an array of 20 elements:
for (i=0; i <= 20; i++)
But I need to fill an array till certain number that the user has entered, for example if User entered 6
then the output must be:
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2
Or if user entered 2, the output must be:
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
Arrays element count needs to be 20, not more, not less.
I get stuck in the place where I need to define that the arrays maximum valued element must be the one the User has written.
Will appreciate any help, thanks!
Have you looked into the Modulo operator? In Java, the modulo operator allows someone to get the remainder of a division. For example: 4 % 3 = 1, and 3 % 3 = 0.
https://en.wikipedia.org/wiki/Modulo_operation
Because this is homework, I’ll give you direction rather than code.
The modulus operator % returns the remainder after division.
Consider how the result of the following operation relates to the value of the desired element at index i:
i % userInput
Modulo operator is your friend here.
Just iterate ( for loop from 0 to 19 ) over array you have to fill and put index % userValue + 1 into it
I tried the Modulo operator and it worked like a charm to solve this problem. And Bohemian, you are right, it is for a homework. Thank you, my question is answered.
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
An integer is a lucky number if each number in it is 3 or 7. To give an example, the numbers 7, 73, and 33777 are lucky numbers. Your task is to calculate the number of lucky numbers between int(a) and int(b).
How could one achieve this efficiently (in less than 1 sec) without going through all the numbers? I have attempted many solutions in Java however none of them are fast enough.
Since you have to count the numbers and not list the numbers, you can use permutation and combination to find the answer.
Eg let say find between 1 and 999 where you can use 3, 7
Then you have 3 lengths single, double and triple digits with constraints on single and triple digits.
For single since minimum number is 1 and 3, 7 both are greater there 2 numbers.
For double digits you have no constraints hence you have 2 * 2 = 4 combinations
Similarly for 3 digits as max number allowed is 9 in each place and 3,7 are lesser than them there will be 2 * 2 * 2 = 8
So answer is 14 after summing them all... This algorithm will run fast as it depends on the size of the numbers to generate ie o(n) time complexity where n is max length of number.
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 6 years ago.
Improve this question
I have this java project(using eclipse) that calculates check digit numbers. I'm stuck on how to code this one.
The check has a check digit so that it makes the sum of the sights including the check digit divisible by 7. Assume there are always 4 digits plus the check digit. The sample is 3875 with the number being 5 Second trial problem is 5862 and check number needs to be found. How do I go about doing this? I got to entering each digit and adding them but how can i do the rest?
This is for an into to computer science class so please no super complex stuff as if we didn't learn it I cant use it.
My teacher sucks by the way we learn none of this. I already did part a I need part b. Thanks. Here's an image to hell clarify.
First of all, you need to develop some "programmer logic", these problems help to develop it.
Airline tickets divide the actual identification number by 7 and assign the remainder to the check digit. Number can be of any length
Example:
12358 #3
Let's break this example:
12358 / 7 = 1765
and the reminder is 3
Let's do the same with the 2nd number on the example:
45349 / 7 = 45346
and the reminder is 3
So, your logic is correct.
An American Express traveler's check has a digit so that it maskes the sum of the digits, including the check digit, evenly divisible by 7.
Example:
3875 #5
In this problem the thing is a little different, you need to sum the digits:
3875 -> 3 + 8 + 7 + 5 = 23
Now you need to get the reminder of 23 / 7
23 / 7 = 3
And a reminder of 2
7 - 2 = 5
That's your checkDigit
5862
5862 -> 5 + 8 + 6 + 2 = 21
21 / 7 = 3
Reminder = 0
checkDigit = 7 - 0 = 7
So the formula is:
Split the number into digits
Sum the digits
Get the mod 7 of the sum
Make a rest of 7 - reminder
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am in 8th grade. I am taking a 12th grade java course. I just started and my home work is to make a Fibonacci series dynamic array program. I don't know where to go. It is online so I can't ask a teacher or something.
How do you make a Fibonacci sequence using a dynamic array in Java?
This was an example of a dynamic array I got:
I don't see how you can make the Fibonacci series out of it! Here is the Fibonacci series example I got.
You can combine the two examples, as such:
Take the DynamicArrayOfInt class, and add the main method of the Fibonacci class.
Insert a new statement at the beginning of the main method instantiating a DynamicArrayOfInt object, as such:
DynamicArrayOfInt arr = new DynamicArrayOfInt();
Replace every instance of numbers[x] with arr.get(x), and instances of numbers[x] = y with arr.put(x, y).
Remove the leftover statements dealing with the numbers array. This will essentially make use of the DynamicArrayOfInt object. A sample output would look like this:
iplante$ java DynamicArrayOfInt
Size of dynamic array increased to: 2
Fibonacci series:
0
1
Size of dynamic array increased to: 4
1
2
Size of dynamic array increased to: 8
3
5
8
13
Size of dynamic array increased to: 16
21
34
55
89
144
233
377
610
Size of dynamic array increased to: 32
987
1597
2584
4181
iplante$
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 was recently asked this question in an interview . Can anyone help me with a code to solve this problem?
We have a sequence of non-zero digits 123456789. The problem is to place plus or minus signs between them so that the result of thus described arithmetic operation will be 100. We can use a number only once . However we can use the operators(+,-,*,/) any number of times
Edit : I was asked to write a Java code for this question . So i believe its relevant
The followup question was to get all possible combinations
Example
Here is an example.
Split the numbers as follows
1 with multiply
4,7,89 with sum
3,6 with sum
4,5 with subtract
3+6 - 4 - 5 = 0
4 + 7 + 89 = 100
1 * 100 = 100
Automatic way of finding all possible combinations.
You essentially have 1 set:
the set {1..9} merged with the set {-,+,/,*,nothing} (nothing being the absence of a symbol)
You need to iterate over all the order possibilities. That will take for a long time. Exclude cases where there are 2 symbols side by side e.g. -/.
I believe this will lead to k-combinations.