Coding Challenge 1: Permutations - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Coding Challenge 1: Permutations (2128 Views)
| Coding Challenge 1: Permutations by Fayimora(op): 9:54am On Jul 06, 2011 |
Hey guys, am going to be dropping some nice coding challenges. You are free to and not to participate. Challenge A Your task is to write code that generates the permutations of a word. So when given the word 'eat' as input, your program should return a list containing eatas output. Time Limit: 1s Goodluck, |
| Re: Coding Challenge 1: Permutations by mxxpunkxx: 3:57pm On Jul 06, 2011 |
*************NO TIME,,,,,,,,,,,LEMME JUST SKETCH (ROUGH WORK)**************** Lets say we wanna generate permutation for the word SLEEVE this aint really hard to do> i would have given the code straight up, but cos of my busy schedule, i'ma just explain what i intend to do, maybe somebody can help spit the code. Permutation means the number of ways letters can be arranged to form a word (whether they make sense or not) . In this case, we have 6 letters in SLEEVE. that 6 * 5 * 4 * 3 * 2 * 1, thats 6! (six factorial), i will loop this (downward counting loop). and the iteration of each value must be multiplied against the value. 1st loop = 6 2nd loop = 6 * 5 = 30 3rd loop = 4 * 30 = 120 4th loop = 3 * 120 = 360 . . . complete the remaining loops yourslves Then its easy to make a factorial function that handles the factorials. this really depnds on whether we are putting order into consideration or not, Formula n_P_k = n! / (n - k)! Thus SLEEVE & permutation 2 will yield, 6_P_2 = 6! / (6 - 2)! = 6! / 4! = 6 x 5 = 30 perms. easily done,,,,create a 2-parameter function 1= number of objects 2= permutations plugging this into the equation along with a couple of calls to the new factorial functions will return the interger value kapish!!!!!!!!!!!!!!! c'est finis!! |
| Re: Coding Challenge 1: Permutations by mxxpunkxx: 4:12pm On Jul 06, 2011 |
************AND HERE COMES THE CODE***************** JAVA************* public class Permutation{ public static void main(String []args) { permute("key" ;} private static void permute(String str) { int length = str.length(); boolean used[] = new boolean[length]; StringBuffer out = new StringBuffer(); char[] in = str.toCharArray(); doPermute(in, out, used, length, 0); } private static void doPermute(char[] in, StringBuffer out, boolean []used, int length, int level) { if(level == length) { System.out.println(out.toString()); return; } for(int i = 0; i < length; i++) { if(used[i]) continue; out.append(in[i]); used[i] = true; doPermute(in, out, used, length, level+1); used[i] = false; out.setLength(out.length()-1); } } } kapish!!!!!!!!!!!!!!! c'est finis!! |
| Re: Coding Challenge 1: Permutations by Nobody: 6:14pm On Jul 06, 2011*. Modified: 2:41am On Apr 07, 2013 |
| Re: Coding Challenge 1: Permutations by Fayimora(op): 6:26pm On Jul 06, 2011 |
No standard libraries Here my code by the way: import java.util.*; It programmed the OO way for reuse, ![]() |
| Re: Coding Challenge 1: Permutations by Nobody: 12:22am On Jul 07, 2011*. Modified: 2:41am On Apr 07, 2013 |
| Re: Coding Challenge 1: Permutations by Fayimora(op): 12:52am On Jul 07, 2011 |
I only understand halft of that code tho. Does it work? I mean dd you try the sample input? |
| Re: Coding Challenge 1: Permutations by Nobody: 1:18am On Jul 07, 2011*. Modified: 2:41am On Apr 07, 2013 |
|
| Re: Coding Challenge 1: Permutations by Fayimora(op): 1:23am On Jul 07, 2011 |
HAhaha naa was just asking if you tried it. Its too easy for u so might not even bother testing. Do the compilers come installed with linux? Cause i kinda like have ll compilers here waitin, Mac is just the best .lol How about calculating the program run time, ? |
| Re: Coding Challenge 1: Permutations by Nobody: 2:31am On Jul 07, 2011*. Modified: 2:41am On Apr 07, 2013 |
| Re: Coding Challenge 1: Permutations by Fayimora(op): 2:45am On Jul 07, 2011 |
All my permutations are in a list Just take out the for-loop in the main method and the ArrayList permutations is your list, public static void main(String[] args) Y arent u online(YM) |
| Re: Coding Challenge 1: Permutations by Nobody: 3:46am On Jul 07, 2011*. Modified: 2:41am On Apr 07, 2013 |
| Re: Coding Challenge 1: Permutations by Fayimora(op): 3:47am On Jul 07, 2011 |
Hmm doesn't linux have ym? or what chat app do u use on linux |
| Re: Coding Challenge 1: Permutations by Nobody: 3:50am On Jul 07, 2011*. Modified: 2:41am On Apr 07, 2013 |
| Re: Coding Challenge 1: Permutations by Fayimora(op): 3:54am On Jul 07, 2011 |
lol, thats like the main stuff i do apart from developing, |
Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) • Mini Web Application Coding Challenge For Programmers • . • 2 • 3 • 4
Changing The Prostitution Industry Through IT • Saas Software Development Company • Computer Science Or Computer Engineering???
;
.lol How about calculating the program run time, ?