Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Ten Lines or Less!

case sensitive

  • Please log in to reply
26 replies to this topic

#1 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 21 August 2009 - 06:54 PM

I issue a challenge!!!

This is for all CodeCall members, and anyone else who thinks they're MAN enough to do it!

The rules are simple, you must produce the program that was issued as a 10 line challenge above, and obviously complete it in 10 lines or less. This could be any program, so make sure you look. Just because it says "Lines" don't think you can be a clever Dan and just pile on statements one behind the other, that "line" limit more accurately means STATEMENT LIMIT. You do not need to write that you accept the challenge, simply post your answer! You may also use any language of your choosing that is not an esoteric language (the one exception to that rule being Lolcode).

You may use any standard library or any commonly recognized and well known library not in the standard (SDL, Qt, etc.), but #include or other similar language constructs are included in the 10 line limit. You can also be as conservative as you want with braces and how big/small your statements are. For example you can stack several if statements one on top of the other if that's the effect you desire. You may also do this with control loops, however, if any of these constructs requires a code block (e.g. brackets), then each statement inside of that code block is also considered a separate statement, for example:
if (anum == 2) return anum; // one statement

if (anum == 3 || anum == 4) return anum - 2;
else anum += 5; // two statements

for (iii = 0; iii < 6; ++iii) {
  if (anum == iii) {
    anum += iii;
    return anum;
  }
  --anum;
} // 5 statements!
(The only exception here is array initialization.)

As soon as there are three answers to the previous challenge, YOU may create your own challenge! In order to issue a challenge, though, you must conform to these rules:
  • You MUST write the program yourself in a language of your choice before issuing the challenge.
  • You must accept any program that follows the LETTER of your challenge, not necessarily the "spirit" of the challenge.
  • You must provide example input and output.

The Challenge:

With that, I issue my challenge. This is the first one, so it will be easy.

Write a program that takes in four arguments in the command line, a string, and three characters. Your program must check if this user input is formatted correctly, and if not it must explain to the user how to format the input properly. It must then locate and print on the command line each location it has found a character, and what character it found. For example, if the user input is this:
myprogram "Test string" t e i
It should print out something similar to this:
Found t at 0
Found e at 1
Found t at 3
Found t at 6
Found i at 8
It should not be case sensitive, it should also be able to take a string of nearly any size and check it. Finally, it should be 10 lines or less!

The catch? No ternary operator, and it MUST be cross-platform.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 relapse

relapse

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 323 posts

Posted 21 August 2009 - 08:48 PM

That is a lot to take in, dude. How about an example?
  • 0

#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 21 August 2009 - 08:57 PM

Here's my completed example, doing the challenge above.
#include <stdio.h>
int main(int argc, char* argv[]) {
    if (argc == 5 && argv[2][1] == '\0' && argv[3][1] == '\0' && argv[4][1] == '\0') {
        char test = argv[1][0], args[] = {argv[2][0], argv[3][0], argv[4][0]};
        for (int iii = 0; iii < 3; ++iii) if (args[iii] > 64 && args[iii] < 91) args[iii] = args[iii] + 32;
        for (int iii = 0; test != '\0'; ++iii, test = argv[1][iii]) {
            if (test > 64 && test < 91) test = test + 32;
            if (test == args[0] || test == args[1] || test == args[2])  printf("Found %c at %i\n", test, iii);}
    } else printf("Incorrect input.\nPlease format your input like this: \"string\" a b c\n");}
Hope that helps, that's the general idea. It's supposed to require a lot of cheap hacks and ignoring things you can overlook to get what you want, as well as rendering it nearly unreadable, but functional! :P
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 22 August 2009 - 03:15 AM

gut!
wheres the next challenge?
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#5 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 22 August 2009 - 06:32 AM

Fun

<?php
if ($argc == 5) {
for ($i = 0; $i<strlen($argv[1]); $i++) {
if ($argv[1][$i] == $argv[2] || $argv[1][$i] == $argv[3] || $argv[1][$i] == $argv[4])
echo "Found {$argv[1][$i]} at position $i\n";
}
} else {
echo "Incorrect input.\nPlease format your input like this: \"string\" a b c\n";
}

Edited by Jordan, 22 August 2009 - 07:22 AM.

  • 0

#6 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 22 August 2009 - 06:49 AM

Again, no new challenge! lol
  • 0

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 22 August 2009 - 06:52 AM

I didn't even notice that last line:

The catch? No ternary operator, and it MUST be cross-platform.



I actually had a ternary operator in one of the versions.
  • 0

#8 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 22 August 2009 - 06:52 AM

JS:
var input="i talk alot";
var search="a,t,o";
var schars=search.split(',');
var chars= input.split('');
var i=0;
chars.each(function(c){
	schars.each(function(sc){
		if(c==sc)alert("found "+sc+" at :"+i);
	});
	i++;
})
use php or whatever server side langauge to replace these values like this:
var input="<?php print $_GET['text'] ?>";
var search="<?php print $_GET['search'] ?>";

  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#9 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 22 August 2009 - 06:54 AM

I do not know enough desktop programming to make anything cross platform haha...

how about making PHP functions or something? I can so do that :D
  • 0

#10 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 22 August 2009 - 06:55 AM

How is PHP not cross platform? I thought about making a recursive function for this challenge but it ended up more than 10 lines.
  • 0

#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 August 2009 - 07:15 AM

C++ version:

#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
if (argc>2)
{
std::string param1 = argv[1];
for (int iii=0;iii<param1.length();iii++)
for(int jjj=2;jjj<argc;jjj++)
{
if (tolower(param1[iii])==tolower(argv[jjj][0])) std::cout<<"Found "<<argv[jjj][0]<<" at "<<iii<<"\n";
}
}
else std::cout<<"Incorrect input: please format your input as \"string\" char char ...\n";
}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#12 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 22 August 2009 - 09:32 AM

Here's my completed example, doing the challenge above.

#include <stdio.h>
int main(int argc, char* argv[]) {
    if (argc == 5 && argv[2][1] == '\0' && argv[3][1] == '\0' && argv[4][1] == '\0') {
        char test = argv[1][0], args[] = {argv[2][0], argv[3][0], argv[4][0]};
        for (int iii = 0; iii < 3; ++iii) if (args[iii] > 64 && args[iii] < 91) args[iii] = args[iii] + 32;
        for (int iii = 0; test != '\0'; ++iii, test = argv[1][iii]) {
            if (test > 64 && test < 91) test = test + 32;
            if (test == args[0] || test == args[1] || test == args[2])  printf("Found %c at %i\n", test, iii);}
    } else printf("Incorrect input.\nPlease format your input like this: \"string\" a b c\n");}
Hope that helps, that's the general idea. It's supposed to require a lot of cheap hacks and ignoring things you can overlook to get what you want, as well as rendering it nearly unreadable, but functional! :P

Not quite cross platform:

main.c: In function `main':
main.c:5: error: 'for' loop initial declaration used outside C99 mode
main.c:6: error: redefinition of 'iii'
main.c:5: error: previous definition of 'iii' was here
main.c:6: error: 'for' loop initial declaration used outside C99 mode
*** Errors occurred during this build ***

:P
  • 0





Also tagged with one or more of these keywords: case sensitive

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download