Increment Database with multiple conditions - java

I'm extremely new to coding, and I need to solve a problem that I'm not sure what is the best approach. I'd appreciate enormously if anyone could point me towards a proper solution!
The problem is: I have a bunch of list of numbers (aprox. 2 thousand) each containing 6 digits, from 1 to 20, like this:
1 {1,13,5,16,4,19};
2 {2,5,9,15,22,8};
3 {14,23,1,13,6};
...
they never repeat within the same list.
I need to add new lists, that should be generated based on information from the 2k previous (I have it in an excel sheet). For example:
The next list (6 numbers, 1 to 20), need to match a given sum (and this sum will be the most frequent sum in my database);
They need to start with a number (for instance,1);
They must contain at least 3 odd numbers;
I have no idea what coding language would be best to use, I'm dedicating my time learning some Python and C#, and see if I can come up with a solution, but I'm really struggling to understand whether these languages are suitable for the problem or not.
Thanks a lot in advance to anyone willing to shed some light on my problem!

Related

Dynamic Programming (how to send a message in the most efficient way)

I'm having a hard time with dynamic programming, I'm new in this, so I hope you could help me with anything you can, the problem is this:
As the Communications Officer of the IKS B'Moth Klingon battle cruiser, your duty is to manage communications in the most efficient way. Assume you need to transmit a message S= s1...sm given as a string of m symbols. For this purpose, you have r different codes. Let b(ij) be the number of bits needed to enconde the i-th symbol of your message in the j-th code. Initially, the bridge transmitter is set to code #1, but you can freely change the code at any point within the message and as many times as you want. To do so, you need to send a control code which is composed of C(ij) bits if you want to switch from current code i to any other code j. Your goal is to determine how to send thee message in the most efficient way (using the least number of bits).
A) Prove the problem exhibits optimal substructure.
B) find a recurrence for the optimal number of bits required.
C) Build a bottom up dynamic programming algorithm to solve the problem and indicate its complexity.
You can make a 3 dimensional array, and use previousCode, newCode, and ithSymbol as indices. The array will store the least number of bits while scanned upto ithSymbol and when it switches code from previousCode to newCode.
The recursive formula will be:
dp(ithSymbol, previousCode, newCode)=min_(i=1 to r)(dp(ithSymbol-1,i,previousCode))+C(previousCode, newCode)+b(ithSymbol,newCode);
(Assumed, C(i,i)=0 for all i)
Now you can write the code for yourself.
N.B. This is a naive approach. You can further make it efficient by making the array 2-D as only ithSymbol-1 is used in any step.

Testing multible combinations

First of all I want to say, that I looked online for a possible or similar solution but did not find anything, might have been looking for the wrong keywords but please excuse my rather simple question.
However, currently I am trying to implement a java function that - on an input of an integer (e.g. 10) gives me every possible solutions on how to divide this by 2 or 1.
And I literally have no clue on how to even start, as there is no pattern to it or anything that I have done is relatable.
However this is an old exam from my professor, so there is a good and short solution I am just not seeing.
Thank you very much for your help and time :)
edit:
The method I am trying to imlement would look as follows:
static void possibilities (int i){ }
I would be able to give an integer (3 for example) and then as output (through Sys.out) all possibilities of all possible combinations of 1 and 2's, for example for 3:
(1,1,1)
(2,1)
(1,2)
Or for 2:
(1,1)
(2)
Here is what I think would work:
say the input is 5 so the very 1st combination will be: (1,1,1,1,1) right? now we add the first two number so it is (2,1,1,1) and store it in a string i.e. "2111" now we find all the permutations of this string (code 4 this is available), now we add the next 1,1 pair so it will look like (2,2,1) i.e "221" and again same process of permutations.
We keep repeating this till the string has just one '1' present or no '1' present, try checking for the number 4, same thing.

query for Math.random to generate only a certain amount of each number needed

I am curious if anyone knows a way to use math.random to generate random numbers between say 0 and 3, But when it generates two 0's or two 1's it rules out the possibility of generating them numbers?
This is for a game assignment for college and all I have left to do is set it that it only generates two of each number with one 3. If anyone knows how this would be very helpful (even if it is using something other than math.random.
The language is Java.
So, you basically want to keep track of the number draws, right?
A possible solution is to use an array whose length is the valid range of your random numbers, where each cell counts the occurrences of the respective number. Then, for each draw, you check the contents of the respective cell in the array to see if you reached the limit. If you did, then redraw and repeat.
Note: at the time of writing this, the language of use is unknown, but the solution is generic enough to be implemented in virtually any language that provide a random() function.

Splitting a singly linked list multiple times

I've been working with linked lists and have been trying to split them. If i had methods to add nodes and to print the list such as A to add and p to print and s to split. I want to split the linked list once or twice or more times at a given index.
For example if I had an input like:
A 1 2 3 4 5 6 7 8
s 2 4 6
p
I'd want my output to be:
1 2
I know how to join split lists but I really want to know how to split them like this, any help would be much appreciated.
¿Have you tried API class LinkedList?
I suggest that you simplify your problem as much as you can by splitting the list at a single element. You probably need a method called split(). What parameters does this method need? What should its return type be? What action does the method do? Are there any side-effects?
In order to create a solution, you need to clearly understand the problem to begin with. Answering these questions can hopefully get you started in the right direction. Let me know what you come up with and we can go from there.

Algorithm to choose random letters for word search game that allows many words to be spelled

I'm making a boggle-like word game. The user is given a grid of letters like this:
O V Z W X
S T A C K
Y R F L Q
The user picks out a word using any adjacent chains of letters, like the word "STACK" across the middle line. The letters used are then replaced by the machine e.g. (new letters in lowercase):
O V Z W X
z e x o p
Y R F L Q
Notice you can now spell "OVeRFLoW" by using the new letters. My problem is: What algorithm can I use to pick new letters that maximizes the number of long words the user can spell? I want the game to be fun and involve spelling e.g. 6 letter words sometimes but, if you pick bad letters, games involve the user just spelling 3 letter words and not getting a chance to find larger words.
For example:
You could just randomly pick new letters from the alphabet. This does not work well.
Likewise, I found picking randomly but using the letter frequencies from Scrabble didn't work well. This works better in Scrabble I think as you are less constrained about the order you use the letters in.
I tried having a set of lists, each representing one of the dies from the Boggle game, and each letter would be picked from a random die side (I also wonder whether I can legally use this data in a product). I didn't notice this working well. I imagine the Boggle dice sides were chosen in some sensible manner, but I cannot find how this was done.
Some ideas I've considered:
Make a table of how often letter pairs occur together in the dictionary. For the sake of argument, say E is seen next to A 30% of the time. When picking a new letter, I would randomly pick a letter based on the frequency of this letter occurring next to a randomly chosen adjacent letter on the grid. For example, if the neighboring letter was E, the new letter would be "A" 30% of the time. The should mean there are lots of decent pairs to use scattered around the map. I could maybe improve this by making probability tables of a letter occurring between two other letters.
Somehow do a search for what words can be spelt on the current grid, taking the new letters to be wildcards. I would then replace the wildcards with letters that allowed the biggest words to be spelt. I'm not sure how you would do this efficiently however.
Any other ideas are appreciated. I wonder if there is a common way to solve this problem and what other word games use.
Edit: Thanks for the great answers so far! I forgot to mention, I'm really aiming for low memory/cpu requirements if possible, I'm probably going to use the SOWPODS dictionary (about 250,000) and my grid will be able 6 x 6.
Here's a simple method:
Write a fast solver for the game using the same word list that the player will use. Generate say 100 different possible boards at random (using letter frequencies is probably a good idea here, but not essential). For each board calculate all the words that can be generated and score the board based on the number of words found or the count weighted by word length (i.e. the total sum of word lengths of all words found). Then just pick the best scoring board from the 100 possibilities and give that to the player.
Also instead of always picking the highest scoring board (i.e. the easiest board) you could have different score thresholds to make the game more difficult for experts.
A minor variation on the letter-pair approach: use the frequency of letter pairs in long words - say 6 letters or longer - since that's your objective. You could also develop a weighting that included all adjacent letters, not just a random one.
This wordgame I slapped up a while back, which behaves very similarly to what you describe, uses English frequency tables to select letters, but decides first whether to generate a vowel or consonant, allowing me to ensure a given rate of vowels on the board. This seems to work reasonably well.
You should look up n-gramming, and Markovian Models.
Your first idea is very losely related to Markovian algorithms.
Basically, if you have a large text corpus, say of 1000 words. What you can do is analyse each letter and create a table to know the probability of a certain letter following the current letter.
For example, I know that the letter Q from my 1000 words ( 4000 letters in total ) is used only 40 times. Then I calculate what probable letters follow using my markov hash table.
For example,
QU happens 100% of the time so I know that should Q be randomly chosen by your application that I need to make sure that the letter U is also included.
Then, the letter "I" is used 50% of the time, and "A" 25% of the times and "O" 25% of the time.
Its actually really complicated to explain and I bet there are other explainations out there which are much better then this.
But the idea is that given a legitmately large text corpus you can create a chain of X letters which are probably consistent with English language and thus should be easy for users to make words out of.
You can choose to look forward on a value of n-gram, the highest the number the easier you could make your game. For example, an n-gram of two would probably make it very hard to create words over 6, but an n-gram of 4 would be very easy.
The Wikipedia explains it really badly, so I wouldn't follow that.
Take a look at this Markov generator:
http://www.haykranen.nl/projects/markov/demo/
I do not know about a precanned algorithm for this, but...
There is a dictionary file in UNIX, and I imagine there is something similar available on other platforms (maybe even in the java libraries? - google it). Anyways, use the files the spell checker uses.
After they spell a word an it drops out, you have existing letters and blank spaces.
1) From each existing letter, go right, left, up, down (you will need to understand recursive algorithms). As long as the string you have built so far is found at the start of words or backwards from the end of words in the dictionary file, continue. When you come across a blank space, count the frequency of the letters you need next. Use the most frequent letters.
It will not guarantee a word as you have not checked the corresponding ending or beginning, but I think it would be much easier to implement than an exhaustive search and get pretty good results.
I think this will get you a step closer to your destination: http://en.wikipedia.org/wiki/Levenshtein_distance
You might look at this Java implementation of the Jumble algorithm to find sets of letters that permute to multiple dictionary words:
$ java -jar dist/jumble.jar | sort -nr | head
11 Orang Ronga angor argon goran grano groan nagor orang organ rogan
10 Elaps Lepas Pales lapse salep saple sepal slape spale speal
9 ester estre reest reset steer stere stree terse tsere
9 caret carte cater crate creat creta react recta trace
9 Easter Eastre asteer easter reseat saeter seater staree teaser
9 Canari Carian Crania acinar arnica canari carina crania narica
8 leapt palet patel pelta petal plate pleat tepal
8 laster lastre rastle relast resalt salter slater stelar
8 Trias arist astir sitar stair stria tarsi tisar
8 Trema armet mater metra ramet tamer terma trame
...

Categories

Resources