Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,286 members, 7,811,850 topics. Date: Sunday, 28 April 2024 at 09:16 PM

Sorting List Of Numbers And Strings: Simple Puzzle - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Sorting List Of Numbers And Strings: Simple Puzzle (9443 Views)

Sorting Out A Simple Text Dictionary With PHP / Sorting Algorithms / How To Sorting Tables With Vb.6 (2) (3) (4)

(1) (2) (Reply) (Go Down)

Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 12:20am On Nov 09, 2012
I came across this a few weeks ago. Here is how it goes:
-----------------

You are to write a program that takes a list of strings containing
integers and words and returns a sorted version of the list.

The goal is to sort this list in such a way that all words are in
alphabetical order and all integers are in numerical order.
Furthermore, if the nth element in the list is an integer it must
remain an integer, and if it is a word it must remain a word.


Input:
------

The input will contain a single, possibly empty, line containing a
space-separated list of strings to be sorted. Words will not contain
spaces, will contain only the lower-case letters a-z. Integers will be
in the range -999999 to 999999, inclusive. The line will be at most 1000
characters long.


Output:
-------

The program must output the list of strings, sorted per the requirements
above. Strings must be separated by a single space, with no leading
space at the beginning of the line or trailing space at the end of the
line.


Constraints:
------------

The code you submit must take input from stdin and produce output to
stdout as specified above. No other output is permitted. You can
assume the input will be valid. Feel free to use standard libraries to
assist in sorting.

In the examples below, the text "Input:" and "Output:" are not part
of the output, and neither are the blank lines.



Example 4:
----------
Input:

car truck 8 4 bus 6 1

Output:

bus car 1 4 truck 6 8
Re: Sorting List Of Numbers And Strings: Simple Puzzle by lordZOUGA(m): 10:08am On Nov 09, 2012
can we use the C++ STL
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 10:10am On Nov 09, 2012
fo sho

i did it using ruby, then converted to java using java collections library

for further context, the idea is to solve this puzzle in ideally 30 minutes or so...so no need to implement sorting from scratch. Using standard library stuff as a building block.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 6:18pm On Nov 10, 2012
http://pastebin.com/rbcJmEaR

my python implementation...took me less than 30 mins tho ...
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 8:53pm On Nov 10, 2012

/*****
* I solved this some 35 hours ago but PHCN wont let me post it mstcheeeww!!! angry
*/

import java.util.*;
public class SortBlaBla {

public static void main(String[] args)
{
//read from statndard input bla bla bla
String input ="1 this a 8 for 7 z you s d 1";
int max = input.length();
Queue theInputQueue = new Queue(max);
Queue theIntQueue = new Queue(max);
Queue theStringQueue = new Queue(max);

String[] firstArray = input.split("\\s"wink;
for(int i=0; i<firstArray.length; i++)
{
theInputQueue.insert(String.valueOf(firstArray[i]));
}
Arrays.sort(firstArray);
for(int i=0; i<firstArray.length; i++)
{
if(isInt(firstArray[i].toString()))
{
theIntQueue.insert((firstArray[i]));
}
else
{
theStringQueue.insert(firstArray[i]);
}
}
while(!theInputQueue.isEmpty())
{
if(isInt(theInputQueue.remove()))
System.out.println(theIntQueue.remove());
else
System.out.println(theStringQueue.remove());

}
}
//Check if a value is an integer
public static boolean isInt(String s)
{
try
{
Integer.parseInt(s);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
}
//you can ignore this part of the code...you can replace it with a STL
class Queue
{
private int maxSize;
private String[] queArray;
private int front;
private int rear;
private int nItems;

Queue(int s)
{
maxSize = s;
queArray = new String[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

void insert(String j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
public String remove()
{
String temp = String.valueOf(queArray[front++]);
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public String peekFront() // peek at front of queue
{
return queArray[front];
}
boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
int size() // number of items in queue
{
return nItems;
}
}
//Boy!...Never new it would be more difficult to post a code than writing the actual code...P.H.C.N i dey hail o! mstcheeew!! angry angry angry angry angry angry
Re: Sorting List Of Numbers And Strings: Simple Puzzle by idesignwebsite(m): 10:35pm On Nov 10, 2012
Javanian:

/*****
* I solved this some 35 hours ago but PHCN wont let me post it mstcheeeww!!! angry
*/

import java.util.*;
public class SortBlaBla {

public static void main(String[] args)
{
//read from statndard input bla bla bla
String input ="1 this a 8 for 7 z you s d 1";
int max = input.length();
Queue theInputQueue = new Queue(max);
Queue theIntQueue = new Queue(max);
Queue theStringQueue = new Queue(max);

String[] firstArray = input.split("\\s"wink;
for(int i=0; i<firstArray.length; i++)
{
theInputQueue.insert(String.valueOf(firstArray[i]));
}
Arrays.sort(firstArray);
for(int i=0; i<firstArray.length; i++)
{
if(isInt(firstArray[i].toString()))
{
theIntQueue.insert((firstArray[i]));
}
else
{
theStringQueue.insert(firstArray[i]);
}
}
while(!theInputQueue.isEmpty())
{
if(isInt(theInputQueue.remove()))
System.out.println(theIntQueue.remove());
else
System.out.println(theStringQueue.remove());

}
}
//Check if a value is an integer
public static boolean isInt(String s)
{
try
{
Integer.parseInt(s);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
}
//you can ignore this part of //the code...you can replace it with //a STL
class Queue
{
private int maxSize;
private String[] queArray;
private int front;
private int rear;
private int nItems;

Queue(int s) // constructor
{
maxSize = s;
queArray = new String[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

void insert(String j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
public String remove()
{
String temp = String.valueOf(queArray[front++]);
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public String peekFront() // peek at front of queue
{
return queArray[front];
}
boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
int size() // number of items in queue
{
return nItems;
}
}
//Boy!...Never new it would be //more difficult to post a code //than writing the actual //code...P.H.C.N i dey hail o! //mstcheeew!! angry angry angry angry angry angry
Helo J help my ignorance,what is the code suppose to solve better still what is the algorithm like..ur olodo student..abeg no vex I don see say na sort you dey sort
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 1:02am On Nov 11, 2012
^^^ sorry, what part of the code do you have problem with?
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 6:45am On Nov 11, 2012
Where is @ekt_bear??
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 7:54am On Nov 11, 2012
Javanian: Where is @ekt_bear??
Beats mee...
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 12:22pm On Nov 12, 2012
@ekt_bear we go fry egg for you
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 2:47pm On Nov 12, 2012
Your implementations are correct (I think).

Mine looked like this: http://pastebin.com/DiJrcbP5
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 7:00am On Nov 13, 2012
Can i see your java implementation?
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 7:34am On Nov 13, 2012
Fo sho

I'll post it when I get home
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 8:01am On Nov 13, 2012
Here is my java version:
http://pastebin.com/DwVpvzAs
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 8:11am On Nov 13, 2012
Nice one...quite similar to mine...
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 8:17am On Nov 13, 2012
Yep....pretty much just a direct translation of my ruby code.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by NoiseMaker1: 1:40pm On Jan 03, 2013
.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by NoiseMaker1: 1:42pm On Jan 03, 2013
₱®ÌИСΞ:
http://pastebin.com/rbcJmEaR

my python implementation...took me less than 30 mins tho ...
is it a website
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Nobody: 2:10pm On Jan 03, 2013
Biko warris this doing on the FP?
Re: Sorting List Of Numbers And Strings: Simple Puzzle by jacob05(m): 6:43pm On Jan 03, 2013
@lordZOUGA i hail ooooo
I'm Quite new to C/C++ but here is my lengthy version...


#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
#include <sstream>

using namespace std;

void split(const string& s, char c,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);

while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);

if (j == string::npos)
v.push_back(s.substr(i, s.length( )));
}
}

bool isIntergerFamily(string value){
int myint =0;
stringstream(value) >> myint;
if (myint != 0){
return true;
}else{
return false;
}
}


template <class T>
void printOut(vector<T> temp ){
for (int i = 0; i < temp.size( ); ++i) {
cout << temp[i] << " ";
}
}

template <class T>
void maskOrder(string type, vector<T> inputVector, vector<string>& maskVector ){
stringstream ss;
for (int i = 0,counter = 0; i < maskVector.size( ) && counter < inputVector.size() ; ++i) {
if(type == "int" && isIntergerFamily(maskVector[i])){
ss.str(""wink;
ss << inputVector[counter];
maskVector[i] = ss.str();
counter++;
}
if(type == "string" && !isIntergerFamily(maskVector[i])){
maskVector[i] = inputVector[counter];
counter++;
}
}
}

int main( ) {
vector<string> splittedString;
vector<string> tempVector;
vector<string> stringVector;
vector<int> intergersVector;
int myint;

string inputString;
cout << "Enter String :";
getline(cin,inputString);
split(inputString, ' ', splittedString);

tempVector = splittedString;
sort(tempVector.begin(),tempVector.end());

for (int i = 0; i < tempVector.size( ); ++i) {
if (isIntergerFamily(tempVector[i])){
stringstream(tempVector[i]) >> myint;
intergersVector.push_back(myint);
}else{
stringVector.push_back(tempVector[i]);
}
}
maskOrder("string",stringVector,splittedString);
maskOrder("int",intergersVector,splittedString);
printOut(splittedString);
}
Re: Sorting List Of Numbers And Strings: Simple Puzzle by jacob05(m): 6:46pm On Jan 03, 2013
Re: Sorting List Of Numbers And Strings: Simple Puzzle by fola14: 7:34pm On Jan 03, 2013
Hello, I am interested in programming, who can help me out. I have very little experience in it and I would be willing to know more. How can I start?
Re: Sorting List Of Numbers And Strings: Simple Puzzle by eaglechild: 10:38pm On Jan 03, 2013
brokoto: Biko warris this doing on the FP?
It tells the world that Nigerians are smart too.
Guess that's what the mod was thinking.
Though he forgot how very esoteric the topic is.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Javanian: 10:50pm On Jan 03, 2013
eaglechild:
It tells the world that Nigerians are smart too.
Guess that's what the mod was thinking.
Though he forgot how very esoteric the topic is.

Everything on Planet Earth is Esoteric...
Re: Sorting List Of Numbers And Strings: Simple Puzzle by lordZOUGA(m): 11:17pm On Jan 03, 2013
@jacob05, nice one boss. Seems as if you learnt coding with "C++ cookbook"... You can do better though
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Nobody: 11:59pm On Jan 03, 2013
Re: Sorting List Of Numbers And Strings: Simple Puzzle by jacob05(m): 12:30am On Jan 04, 2013
lordZOUGA: @jacob05, nice one boss. Seems as if you learnt coding with "C++ cookbook"... You can do better though
yes boss wink
Re: Sorting List Of Numbers And Strings: Simple Puzzle by InvertedHammer: 4:18am On Jan 04, 2013
This is a programming question for beginners!

Nice try though
Re: Sorting List Of Numbers And Strings: Simple Puzzle by pak: 4:45am On Jan 04, 2013
ekt_bear, I feel like punching you for depriving me of my sleep really. Saw this as I was about to hit the sack and couldn't blink until I cracked it.

My fav pastime is Python but since I dont us python in my day job, I've been terribly rusty.
Infact the stuff took me well over an hour considering I was even just installing the python executable on my new sis.

However, having lost out in the competition to be the fastest, I strove for the next glorious crown - to be the most elegant grin grin grin

Guess I achieved it. 19 lines of code

Prince actually did the stuff in 18 lines (with the white spaces stripped of) but the script didn't run when I tried it out. I guess he has to check out one or two stuffs in the code.

. . .and the Java solutions ? puleeeeeeeeaaaaaaaaaase. is that the code to calculate the GDP of Nigeria ?
Really, I appreciate you guys (the programmers) but its the lang that I detest.

To The C++ guy, I believe C++ can do better than that, That was actually my first love. but kudos though.



Check out my solution

http://pastebin.com/W3swZVfd

(1) (2) (Reply)

How Do I Edit Datagrid ? / Teach Yourself To Code / Is Anything Better Than The Netbeans Ide?

(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. 50
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.