Skip to main content

Timeline for AlTeRnAtE tHe CaSe

Current License: CC BY-SA 3.0

18 events
when toggle format what by license comment
May 27, 2017 at 13:30 history edited daniero CC BY-SA 3.0
-6 bytes
May 27, 2017 at 0:19 comment added RJHunter @Ventero Since the challenge limits input to ASCII, &95 should be safely equivalent to &~32 to clear the lowercase bit.
May 26, 2017 at 22:25 comment added Ventero @daniero: You first need to clear out bit 5 before toggling it with 32*$. to alternate between upper and lower case. After fiddling a bit with operator precedence, I think the shortest you can get with that approach is 42: gsub(/\p{L}/){($&.ord&~32|32*$.^=1).chr}. Unfortunately, Integer only has a [] method without corresponding []=, otherwise you could just set the bit directly ...
May 26, 2017 at 14:01 history rollback daniero
Rollback to Revision 3
May 26, 2017 at 13:39 history edited daniero CC BY-SA 3.0
-2 bytes
May 26, 2017 at 5:58 comment added RJHunter @daniero I'm sure you'll spot it when you're fresher, but the AND-NOT technique only works to unset the lowercase bit (upcase); you'd need OR to set the lowercase bit on an uppercase letter (so it can cover downcase). Switching between Boolean operators could be done cleverly but probably no shorter than switching between string methods.
May 26, 2017 at 3:48 comment added Cyoce instead of the BEFORE{i=0} thing you mentioned in a previous comment, you could do i||=0
May 25, 2017 at 23:47 comment added daniero @Ventero Hey, I got another Idea. Doesn't quite work yet -- Work in progress (really got to go to bed now): gsub(/\p{L}/){($&.ord&~(32*($.^=1))).chr} .. Too tired to see where the error is
May 25, 2017 at 23:34 history edited daniero CC BY-SA 3.0
-many bytes
May 25, 2017 at 23:24 comment added Ventero Turns out I lied, I have another comment :D As the input is guaranteed to only ever contain printable ASCII, we can use Ruby's support for Unicode categories in regular expressions: /\p{L}/ (Unicode category Letter) is one character shorter than /[a-z|/i.
May 25, 2017 at 23:04 comment added Ventero Final remark, I promise :) Once you're using -p0, you can actually save a few more characters in how you flip $. back and forth: Since it's now guaranteed to be 1 when your code is invoked, you can simply use $.^=1.
May 25, 2017 at 22:56 comment added Ventero Another thing about the command line flag: -p0 makes the interpreter read all available input in one go - so your code is only invoked once, allowing you to freely use $.. Combining that with the fact that gsub implicitly operates as $_.gsub! when specifying -p makes a full program significantly shorter: 48 characters for gsub(/[a-z]/i){[$&.upcase,$&.downcase][1&$.+=1]} and 2 for the p0 flag.
May 25, 2017 at 22:53 history edited daniero CC BY-SA 3.0
-2 bytes
May 25, 2017 at 22:47 comment added Ventero 1&$.+=1 allows you to drop the parentheses. And for completeness' sake, there is another global integer - it's unfortunately just read-only: $$.
May 25, 2017 at 21:22 comment added Value Ink Yeah, the problem is that you definitely need to work with multiple inputs with that solution in order to save space. Thankfully the -p flag usually counts as 1 byte by consensus, because ruby -pe 'your code' has an edit distance of 1 from ruby -e 'your code' so it is a tie with your solution at best.
May 25, 2017 at 21:11 comment added daniero @ValueInk Hah didn't even think of using the -p switch, but yeah, that one's hard to get around if had to make it work for multiline inputs (which I think you should in this case). You could add the line $.+=1 on top to make it work, but then you're up to 56 bytes excluding the -p flag. And BEFORE{i=0} is way too long. Why isn't there another global integer? :D
May 25, 2017 at 20:51 comment added Value Ink Man, if it weren't for the fact that $. auto-increments with each gets call, a full program with the -p flag would've been shorter...
May 25, 2017 at 20:42 history answered daniero CC BY-SA 3.0