Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,334 members, 7,811,974 topics. Date: Monday, 29 April 2024 at 03:29 AM

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

Nairaland Forum / Science/Technology / Programming / Sorting List Of Numbers And Strings: Simple Puzzle (9444 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)

Re: Sorting List Of Numbers And Strings: Simple Puzzle by pak: 5:12am On Jan 04, 2013
InvertedHammer: This is a programming question for beginners!

Nice try though






Omo elefo , who ask you ?

ekt_bear didn't ask whether it was for novices or superman, all he said was solve it !!!
Re: Sorting List Of Numbers And Strings: Simple Puzzle by ektbear: 6:03am On Jan 04, 2013
Heh, funny that this made the front page several months after.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by eaglechild: 6:03am On Jan 04, 2013
Javanian:

Everything on Planet Earth is Esoteric...
You realise i used the word 'very' right.
Besides front page topics should appeal to a wider range of audience.
If i come out ranting about my subspeciality in medicine, i wonder how many people will contribute.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by lordZOUGA(m): 7:52am On Jan 04, 2013
jacob05: @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);
}

this code breaks if the input contains zero
Re: Sorting List Of Numbers And Strings: Simple Puzzle by jacob05(m): 7:57am On Jan 04, 2013
pak: 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
Well, thanks bro. My C++ version could have been 'elegant' as you claim your solution is if C++ had provided robust string processing classes like python does (li.split() wink) and also a portable mechanism to check for type (s.isdigit() python provided). Guessed those took the glory smiley. I'm still waiting for a better solution though
Re: Sorting List Of Numbers And Strings: Simple Puzzle by jacob05(m): 8:33am On Jan 04, 2013
lordZOUGA:
this code breaks if the input contains zero
Nice catch boss! Fixed

#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;
return ((myint != 0) || (value == "0" ) ) ? true : 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("" ;
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): 8:48am On Jan 04, 2013
pastebin.com/rWhEvaYw
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 10:14am On Jan 04, 2013
pak: 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

My logic is undeniable! tongue I can evn make it shorter....
Lmao @ GDP of Nigeria....
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Nobody: 2:50pm On Jan 04, 2013
Re: Sorting List Of Numbers And Strings: Simple Puzzle by lordZOUGA(m): 3:04pm On Jan 04, 2013
and then the contest turns to a test of code length..
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 3:07pm On Jan 04, 2013
omo_to_dun:

Haba! You removed all the empty lines and cheated your way to 19 lines. Well this is mine and it is 15 lines.



Haba! You removed all other lines leaving only the your method and cheated your way to 15 lines. grin

l =  "car truck 8 4 bus 6 1"
lint, lalph, lnew = [],[],[]
for i in l.split():
if i.isdigit():
lint.append(i)
else:
lalph.append(i)
lalph.sort(reverse=True)
lint.sort(reverse=True)
for i in l.split():
if i.isdigit():
lnew.append(lint.pop())
else:
lnew.append(lalph.pop())
print " ".join(map(str, (lnew)))
Re: Sorting List Of Numbers And Strings: Simple Puzzle by lordZOUGA(m): 7:15pm On Jan 04, 2013
another C++ implementation http://pastebin.com/KbgsKFiG
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Nobody: 8:17pm On Jan 04, 2013
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 11:20pm On Jan 04, 2013
omo_to_dun: @PRINCE

I am not denying it. I was only making a point. I wanted to show that if we all removed the empty lines and made some clever adjustments, our code would be small.
owky...true that
Re: Sorting List Of Numbers And Strings: Simple Puzzle by pak: 12:47am On Jan 05, 2013
lordZOUGA: and then the contest turns to a test of code length..

Not a big deal bro,

Many of the guys on the thread have already gotten the logic of the solution and prince did it in less than 30 mins to boot.

It was just a way of introducing more fun to the whole process. No biggie !


I don't know if you are a pythonista, so maybe just maybe you might not understand the intense pleasure of writing elegant minimalist code.


I got hooked on the whole concept bout four years back when I saw a contest on the web to write the shortest code to extract youtube videos as flv.

Definitely, Perl came first (with about just 2 lines or so), awk followed and Python was third.
Re: Sorting List Of Numbers And Strings: Simple Puzzle by pak: 12:59am On Jan 05, 2013
omo_to_dun: @PRINCE

I am not denying it. I was only making a point. I wanted to show that if we all removed the empty lines and made some clever adjustments, our code would be small.

Bro, the reason why I didn't mention yours was cos as much as your logic might have been right. The code still didn't meet up with what ekt_bear specified, ability to receive input from stdin and output to stdout, for obvious reasons



l =  "car truck 8 4 bus 6 1"
lint, lalph, lnew = [],[],[]
for i in l.split():
if i.isdigit():
lint.append(i)
else:
lalph.append(i)
lalph.sort(reverse=True)
lint.sort(reverse=True)
for i in l.split():
if i.isdigit():
lnew.append(lint.pop())
else:
lnew.append(lalph.pop())
print " ".join(map(str, (lnew)))



@Prince - cool bro - 15 lines,
now let me see how far I can refactor mine
Re: Sorting List Of Numbers And Strings: Simple Puzzle by pak: 1:52am On Jan 05, 2013
I went on an overdrive and came up with a solution that will definitely take something special to beat.

6 lines !!!.

The only thing I can say is, PYTHON IS SWEET


You can check it out !


li = raw_input("enter "wink.split(' ')
intarr,wordarr,finalarr = [],[],[]
[intarr.append(elem) if elem.isdigit() else wordarr.append(elem) for elem in li ]
wordarr.sort()
intarr.sort()
print [intarr.pop(0) if elem.isdigit() else wordarr.pop(0) for elem in li ]
Re: Sorting List Of Numbers And Strings: Simple Puzzle by PrinceNN(m): 3:04am On Jan 05, 2013
pak: I went on an overdrive and came up with a solution that will definitely take something special to beat.

6 lines !!!.

The only thing I can say is, PYTHON IS SWEET


You can check it out !


li = raw_input("enter "wink.split(' ')
intarr,wordarr,finalarr = [],[],[]
[intarr.append(elem) if elem.isdigit() else wordarr.append(elem) for elem in li ]
wordarr.sort()
intarr.sort()
print [intarr.pop(0) if elem.isdigit() else wordarr.pop(0) for elem in li ]




lool i have similar code (6lines) too...was just waitn for someone to up the limit grin....but id give it to you as urs contained less characters
Re: Sorting List Of Numbers And Strings: Simple Puzzle by Fayimora(m): 3:41am On Jan 05, 2013
I was solving this with Java but stopped when I really understood the question. Scala and Functional Programming FTW!!! https://gist.github.com/4459253
Time spent: 10-12mins

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