Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active January 25, 2017 11:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeGolfScotland/6beb7b07a12608cf520d9af0c95310a8 to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/6beb7b07a12608cf520d9af0c95310a8 to your computer and use it in GitHub Desktop.
The June 2016 challenge

June 2016 - sillyCASE

Task:

Create a function that takes a string and returns that string with the first half lowercased and the last half uppercased.

Example:

foobar => fooBAR

If the string length is an odd number then 'round' it up to find which letters to uppercase. See example below.

sillycase("brian")  
//         --^-- midpoint  
//         bri    first half (lower-cased)  
//            AN second half (upper-cased)  

About Code Golf Scotland

@cwhite92
Copy link

cwhite92 commented Jun 4, 2016

PHP, 90 88 80 93 bytes

So I made the assumption that PHP warnings aren't allowed, and I should write a function that takes the complete string and returns the complete string.

function s($s){$a=str_split($s,(strlen($s)+1)/2);return strtolower($a[0]).strtoupper($a[1]);}

In attempting another method involving a loop I came up with this, but it's not as short (105 bytes):

function sillycase($s){foreach(str_split($s)as$p=>$c){@$n.=$p>=strlen($s)/2?strtoupper($c):$c;}return$n;}

@CraMo7
Copy link

CraMo7 commented Jun 4, 2016

Language: JavaScript (es2015)
Length: 80
Solution:

s=>s.split("").map((c,i)=>i<s.length/2?c.toLowerCase():c.toUpperCase()).join("")

Copy link

ghost commented Jun 5, 2016

Language: Haskell
Length: 89 75
Solution:

s x = 
  let (l, r) = splitAt (succ(length x) `div` 2) x
  in map toLower l ++ map toUpper r

Reduced:

s x=let(l,r)=splitAt(succ(length x)`div`2)x in map toLower l++map toUpper r

@rawkode
Copy link

rawkode commented Jun 5, 2016

Language: Elixir
Length: 95
Solution

alias String,as: S;fn s->{a,b}=S.split_at(s,round(S.length(s)/2));S.downcase(a)<>S.upcase(b)end

@dannyockilson
Copy link

dannyockilson commented Jun 5, 2016

Language: Python
Length: 58
Solution:

def f(s):
 m=(len(s)+1)//2
 return s[:m].lower()+s[m:].upper()

@rawkode
Copy link

rawkode commented Jun 5, 2016

Language: Perl
Length: 59 87 85 74
Solution:

sub a{$_=pop;$n=$==(length()+1)/2;@a=unpack("(A$n)*");lc(@a[0]).uc(@a[1])}

@billythekid
Copy link

billythekid commented Jun 6, 2016

Great idea guys, love me a bit of code golf, even though I'm rubbish at it. ;oP

Language: JS
Length: 116
Solution

function s(w){b=[]
x=w.length
for(i=0;i<x;i++)b.push(i<x/2?w[i].toLowerCase():w[i].toUpperCase())
return b.join('')}

@martinsvalin
Copy link

martinsvalin commented Jun 6, 2016

Language: Ruby
Length: 51
Solution:

->s{s.downcase.sub(/(.{#{s.size/2}}$)/){$1.upcase}}

or

->s{h=(s.size+1)/2;s[0,h].downcase+s[h..-1].upcase}

@AndyGaskell
Copy link

AndyGaskell commented Jun 15, 2016

Language: Tcl
Length: 86
Solution:

proc g {s} {return [string tolower [string toupper $s] 0 [expr [string length $s]/2]]}

@AndyGaskell
Copy link

AndyGaskell commented Jun 16, 2016

I thought PHP was gonna be a tough one, so, not looking at other comments, the best I came up with was...

Language: PHP
Length: 98
Solution:

function g($s){$l=(strlen($s)/2)+.5;return strtolower(substr($s,0,$l)).strtoupper(substr($s,$l));}

...and...
Language: PHP
Length: 124
Solution: though there is a nice quiet bug in this :)

function g($s){$l=(strlen($s)/2)+.5;$s=strtolower($s);$s=str_replace(substr($s,$l),strtoupper(substr($s, =$l)),$s);return $s;}

...and...
Language: PHP
Length: 130
Solution:

function g($s){$r="";$a=str_split(strtolower($s));foreach($a AS $i=>$x){$r.=($i<(count($a)/2))?$a[$i]:ucfirst($a[$i]);}return $r;}

...and...
Language: PHP
Length: 139
Solution:

function g($s){$s=strtolower($s);$a=str_split($s);foreach($a AS $i=>$x){if($i>=(count($a)/2)){$a[$i]=ucfirst($a[$i]);}}return implode($a);}

@billythekid
Copy link

billythekid commented Jun 30, 2016

Language: PHP
Length: 81 93 90 94
Solution

function s($s){$a=str_split($s,(strlen($s)+1)/2);return strtolower($a[0]).strtoupper($a[1]);}

[edit] hah, this is the exact same as @cwhite92's answer above, even down to the variable names!! lmao, gmta. Ok here's a slightly improved version, for 90:

function s($s){$a=str_split(strtolower($s),1+strlen($s)/2);return$a[0].strtoupper($a[1]);}

and since both of those are buggy for even-length strings, here's a clean 94:

function s($s){$a=str_split(strtolower($s),ceil(strlen($s)/2));return$a[0].strtoupper($a[1]);}

okay here's a for loop for 121 cheekily using array notation on a string instead of splitting it out...

function s($s){$b='';$t=strlen($s);for($k=0;$k<$t;$k++){$l=$s[$k];$b.=$k<$t/2?strtolower($l):strtoupper($l);}return$b;}

here's another php version using a bunch of array functions, 116 chars…

function s($s){$s=str_split(strtolower($s));$u=strtoupper(join(array_splice($s,1+count($s)/2)));return join($s).$u;}

(or this bug-free version of that at 120, bugs are just features though right? who codes to a spec anyway…)

function s($s){$s=str_split(strtolower($s));$u=strtoupper(join(array_splice($s,ceil(count($s)/2))));return join($s).$u;}

…not sure why but I quite like this one, i think because we do the upper bit first and work on half the word at a time rather than each letter. In at 118…

function s($s){$a=str_split(strtoupper($s),ceil(strlen($s)/2));return str_ireplace($a[0],strtolower($a[0]),join($a));}

@alexwaeseperlman
Copy link

alexwaeseperlman commented Jul 1, 2016

Language: Python
Length: 58 now

def a(s):
    a=len(s)//2
    return s[:a].lower()+s[a:].upper()

It doesn't look like 58 characters because of the tabs and the new lines, but you can type this into the developer console:

`def a(s):
    a=len(s)//2
    return s[:a].lower()+s[a:].upper()`.length

and it prints 58

BTW dannywilson's is actually 62, you can see by doing this in the console:

`def f(s):
 m=(len(s)+1)//2
 return s[:m].lower()+s[m:].upper()`.length

If you don't count new lines or spaces dannywilson's is 58, but if I don't count them I have 54

@joaquinferrero
Copy link

joaquinferrero commented Jul 1, 2016

Language: Perl 5
Length: 57 56
Solution:

sub f{substr$_=lc pop,-(length>>1),0,'\U';eval"qq($_)"}

@joaquinferrero
Copy link

joaquinferrero commented Jul 1, 2016

Language: Perl 6
Length: 53 47

sub f{$_=$^a.lc;.substr-rw(.5+.chars×½).=uc;$_}

Demo:

$ perl6 -e 'say f("bRian"); sub f{$_=$^a.lc;.substr-rw(.5+.chars×½).=uc;$_}'
briAN

@salathe
Copy link

salathe commented Jul 1, 2016

It's not June any more, but here's a sneaky wee PHP one.

Language: PHP
Length: 88

function s($a){$o=-strlen($a)/2;return substr_replace($a,substr(strtoupper($a),$o),$o);}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment