[Snark] Python procedure

Brian Chan submissions at badcode.rocks
Wed Oct 24 23:53:33 UTC 2018


== License ==

This is my own work. Let's license it under CC-BY, thanks!

== pig.py ==

#!/usr/bin/python3

import sys

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'


def convert(s):
  # rule 1
  if s[0] in VOWELS or s.startswith('xr') or s.startswith('yt'):
    return s + 'ay'

  # rule 3
  if s[0:2] == 'qu':
    return s[2:] + s[0:2] + 'ay'
  if s[0] in CONSONANTS and s[1:3] == 'qu':
    return s[3:] + s[0:3] + 'ay'

  # rule 2
  if len(s) >= 2 and s[0] in CONSONANTS and s[1] in VOWELS:
    return s[1:] + s[0] + 'ay'
  if len(s) >= 3 and s[0] in CONSONANTS and s[1] in CONSONANTS and s[2] in VOWELS:
    return s[2:] + s[0:2] + 'ay'
  if len(s) >= 4 and s[0] in CONSONANTS and s[1] in CONSONANTS and s[2] in CONSONANTS and s[3] in VOWELS:
    return s[3:] + s[0:3] + 'ay'

  # rule 4
  if len(s) >= 2 and s[0] in CONSONANTS and s[1] == 'y':
    return s[1:] + s[0] + 'ay'
  if len(s) >= 2 and s[0] in CONSONANTS and s[1] in CONSONANTS and s[2] == 'y':
    return s[2:] + s[0:2] + 'ay'
  if len(s) >= 2 and s[0] in CONSONANTS and s[1] in CONSONANTS and s[2] in CONSONANTS and s[3] == 'y':
    return s[3:] + s[0:3] + 'ay'
  
  return s


if __name__ == '__main__':
  sentence = sys.argv[1]
  words = sentence.split(' ')
  new_words = map(convert, words)
  new_sentence = ' '.join(new_words)
  print(new_sentence)


More information about the Snark mailing list