Post AQ8Jt6iNl2DpnKr596 by postmodern@ruby.social
(DIR) More posts by postmodern@ruby.social
(DIR) Post #AQ7vh9GE4JVfJdRxXU by postmodern@ruby.social
2022-11-30T03:27:43Z
0 likes, 1 repeats
Fun Programming Challenge: write a function that randomizes the case of the characters in a String, but ensures at least one character has a different case than the input String. (i.e. `random_case("foo")` should never return "foo".)
(DIR) Post #AQ7vh9jeIv2smtDSOu by james@ruby.social
2022-11-30T10:46:48Z
0 likes, 0 repeats
@postmodern my attempt, trying to capture the spirit of the intention so it's comprehensible:def random_case(string) chars_to_flip = (0...string.length).to_a.shuffle.take(rand(string.length) + 1) string.chars.map.with_index { |c,i| characters_to_flip.include?(i) ? c.upcase : c }.joinendWe can guarantee that the string is never the same by ensuring that the set of random character indexes selected for flipping always contains at least one element.
(DIR) Post #AQ7xyTtWnzAu8rE4Gm by MichaelT@ruby.social
2022-11-30T11:12:22Z
0 likes, 0 repeats
@james @postmodern typo alert: the variable 'chars_to_flip' became 'characters_to_flip' ... but it works fine. Output from a few sample runs, since I have 500 characters here:ThE qUick BrOwN foX JumPs OveR tHe LAZy doGTHE QUICK BROWN FOx JUMPS OVER THE LAZY DOGTHE quick browN fox Jumps OVeR THE lazY dogthe quick BROwn foX jumps over tHe laZy dog
(DIR) Post #AQ7yKxrG5BeLRUswGe by james@ruby.social
2022-11-30T11:16:26Z
0 likes, 0 repeats
@MichaelT @postmodern thanks, fixed :) And your tests suggest to me a further requirement: at least one character _in every word_ should be flipped 😄
(DIR) Post #AQ7yTroVvNRXIpqXcu by MichaelT@ruby.social
2022-11-30T11:18:03Z
0 likes, 0 repeats
@james @postmodern yeah, that second line of output really threw me for a few seconds. 🤔
(DIR) Post #AQ7ybd6uTUxa1B9Wa0 by james@ruby.social
2022-11-30T11:19:27Z
0 likes, 0 repeats
@MichaelT @postmodern and further, we might say that we want _at least one_ but _never all_ characters to be flipped in case. Anyway, looking forward to seeing my amazing code in production soon 😆
(DIR) Post #AQ7ygihf6mWeoNlp68 by MichaelT@ruby.social
2022-11-30T11:20:20Z
0 likes, 0 repeats
@james 😂 😂 😂
(DIR) Post #AQ89GiSiTtA41xaBUW by dblack@ruby.social
2022-11-30T12:29:59Z
0 likes, 0 repeats
@postmodern I'm a little late to the party but I'm curious how people would suggest testing this (any implementation), without getting into a prove-the-negative situation.
(DIR) Post #AQ89Giv4mRqXRuqphA by dblack@ruby.social
2022-11-30T12:56:49Z
0 likes, 0 repeats
@postmodern Maybe do a three-character string several thousand times? (I seem to be in a brute force mood this morning but I'm not sure how else one would test it never coming up with the same string, except maybe by stubbing some built-in string methods.)
(DIR) Post #AQ89GjXMU6THMelPN2 by james@ruby.social
2022-11-30T13:18:54Z
0 likes, 0 repeats
@dblack @postmodern I'd certainly do something like that.
(DIR) Post #AQ89SC3qm0KR11QNaC by dblack@ruby.social
2022-11-30T13:21:00Z
0 likes, 0 repeats
@james @postmodern I remember discussing with Gregory Brown the matter of how to test that a class modeling a deck of cards successfully shuffled the deck. My feeling was that if the deck ever came back in the same order, that was evidence that the shuffle method wasn't working. Greg made the point that it was (though barely) possible for a shuffle to return the same order. I don't remember any particular resolution but it was an interesting discussion.
(DIR) Post #AQ8ACiT4cM7wqPPFIW by james@ruby.social
2022-11-30T13:29:24Z
0 likes, 0 repeats
@dblack @postmodern I think if I absolutely _had_ to have a deterministic test, I'd use a set of seed values for the randomiser, and a known set of "shuffled" outcomes for each seed.But what I would actually want to write is:number_of_iterations_that_makes_me_confident = 1000number_of_iterations_that_makes_me_confident.times do # do the thing, assert it workedend... then if it ever fails, another developer reading that test will have much more context.
(DIR) Post #AQ8DjfY4nB9lD19QHI by kuba@ruby.social
2022-11-30T14:08:58Z
0 likes, 0 repeats
@james @dblack @postmodern I would probably test this as:it converts one lowercase character string to uppercaseit converts one uppercase character to lowercaseand thenit produce different string, yet source lowercased equal lowercased product
(DIR) Post #AQ8Jt6iNl2DpnKr596 by postmodern@ruby.social
2022-11-30T15:17:46Z
0 likes, 0 repeats
@james this is also what I was thinking. You would need to select +1 random (but unique) indexes to flip to ensure that at least one index is always flipped. Doing bitflipping with rand() might still result in no indexes being flipped, because there's still a small probability that rand() might return 0.