Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,741 members, 8,000,129 topics. Date: Monday, 11 November 2024 at 10:35 PM

Coding Challenge 1: Permutations - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Coding Challenge 1: Permutations (2067 Views)

Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) / Mini Web Application Coding Challenge For Programmers / . (2) (3) (4)

(1) (Reply) (Go Down)

Coding Challenge 1: Permutations by Fayimora(m): 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

eat
eta
aet
ate
tea
tae
as 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"wink;

}

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
Re: Coding Challenge 1: Permutations by Fayimora(m): 6:26pm On Jul 06, 2011
No standard libraries

Here my code by the way:
import java.util.*;
class PermutationGenerator
{
private String word;

public static void main(String[] args)
{
PermutationGenerator generator = new PermutationGenerator("eat"wink;
ArrayList<String> permutations = generator.getPermutations();
for(String s: permutations)
{
System.out.println(s);
}
}

public PermutationGenerator(String word)
{
this.word = word;
}

public ArrayList<String> getPermutations()
{
ArrayList<String> permutations = new ArrayList<String>();

//base case for empty string
if(word.length() <= 1)
{
permutations.add(word);
return permutations;
}

for (int i=0; i<word.length(); i++)
{
//form simpler word by removing ith character
String shorterWord = word.substring(0,i) + word.substring(i+1);
//generate all permutations from the shorterword
PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(shorterWord);
ArrayList<String> shorterWordPermutations = shorterPermutationGenerator.getPermutations();

//add the removed character to the front of each permutation of the simpler word
for(String s: shorterWordPermutations)
{
permutations.add(word.charAt(i)+s);
}
}
return permutations;
}
}


It programmed the OO way for reuse, grin
Re: Coding Challenge 1: Permutations by Nobody: 12:22am On Jul 07, 2011
Re: Coding Challenge 1: Permutations by Fayimora(m): 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

Re: Coding Challenge 1: Permutations by Fayimora(m): 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 cheesy .lol How about calculating the program run time, ?
Re: Coding Challenge 1: Permutations by Nobody: 2:31am On Jul 07, 2011
Re: Coding Challenge 1: Permutations by Fayimora(m): 2:45am On Jul 07, 2011
All my permutations are in a list cheesy Just take out the for-loop in the main method and the ArrayList permutations is your list,
public static void main(String[] args)
{
PermutationGenerator generator = new PermutationGenerator("eat"wink;
ArrayList<String> permutations = generator.getPermutations();


}

Y arent u online(YM)
Re: Coding Challenge 1: Permutations by Nobody: 3:46am On Jul 07, 2011
Re: Coding Challenge 1: Permutations by Fayimora(m): 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
Re: Coding Challenge 1: Permutations by Fayimora(m): 3:54am On Jul 07, 2011
lol, thats like the main stuff i do apart from developing,

(1) (Reply)

C/C++ Doctor: Get Help In Writing C/C++ Programs / 7 Steps How To Create Android App With No Coding Skills? / Meet The First Nigerian To Break iOS History

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.