Guest User

Bad Code Coding Challenge #13 - To Weird Case

a guest
Jun 28th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2.  
  3. namespace WeirdCase
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             test_and_time("The quick brown fox jumped over the lazy dog");
  10.  
  11.             test_and_time("What the fuck did you just fucking say about me, you little bitch? I'll have you know I graduated top of my class in the Navy Seals, and I've been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. You're fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that's just with my bare hands. Not only am I extensively trained in unarmed combat, but I have access to the entire arsenal of the United States Marine Corps and I will use it to its full extent to wipe your miserable ass off the face of the continent, you little shit. If only you could have known what unholy retribution your little 'clever' comment was about to bring down upon you, maybe you would have held your fucking tongue. But you couldn't, you didn't, and now you're paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. You're fucking dead, kiddo.");
  12.         }
  13.        
  14.         static void test_and_time(String x)
  15.         {
  16.             DateTime time_start = DateTime.Now;
  17.  
  18.             Console.WriteLine(to_weird_case(x));
  19.  
  20.             DateTime time_stop = DateTime.Now;
  21.             TimeSpan duration = time_stop - time_start;
  22.             Console.WriteLine("(" + (int)duration.TotalMinutes + " minutes, " + duration.Seconds + " seconds, and " + duration.Milliseconds + " milliseconds)");
  23.  
  24.             Console.ReadLine();
  25.         }
  26.  
  27.         static String to_weird_case(String x)
  28.         {
  29.             char[] xs = x.ToCharArray();
  30.             for (uint i = 0; i < xs.Length; i++)
  31.             {
  32.                 if (is_even(i)) xs[i] = char.ToLower(xs[i]);
  33.                 else xs[i] = char.ToUpper(xs[i]);
  34.             }
  35.             return new String(xs);
  36.         }
  37.  
  38.         static bool is_even(uint n)
  39.         {
  40.             /*
  41.              * Goldbach's conjecture is known to be true well past 2^32, so all we have
  42.              * to do is exhaustively search for two primes (each greater than 2) that
  43.              * add up to n. If we find any, then n is even, otherwise odd. We have to
  44.              * handle the first few cases manually.
  45.              */
  46.             if (n == 0) return true;
  47.             else if (n == 1) return false;
  48.             else if (n == 2) return true;
  49.             else if (n == 3) return false;
  50.             else if (n == 4) return true;
  51.             else
  52.             {
  53.                 for (uint i = 3; i < n; i++)
  54.                 {
  55.                     if (is_prime(i))
  56.                     {
  57.                         for (uint j = i; j < n; j++)
  58.                         {
  59.                             if (is_prime(j))
  60.                             {
  61.                                 if (i + j == n) return true;
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.                 return false;
  67.             }
  68.         }
  69.  
  70.         static bool is_prime(uint n)
  71.         {
  72.             /*
  73.              * We can determine if n is a prime by exhaustively searching for
  74.              * numbers less than n that, when repeatedly subtracted from n,
  75.              * eventually reach 0. If we find one, then n is composite, but
  76.              * if we don't, then n is prime.
  77.              */
  78.             for (uint m = 2; m < n; m++)
  79.             {
  80.                 uint k = n;
  81.                 while (k >= m) k -= m;
  82.                 if (k == 0) return false;
  83.             }
  84.             return true;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment