Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Created June 18, 2020 20:25
Show Gist options
  • Save pyrofolium/aff74b43d4ea3383d1774e4dd1887dec to your computer and use it in GitHub Desktop.
Save pyrofolium/aff74b43d4ea3383d1774e4dd1887dec to your computer and use it in GitHub Desktop.
# You have been provided a file with a list of strings. Each string is on a new line.
# The size of the file could be anywhere between 5MB - 1000MB.
# Write a program/script to count and print lines which have the following pattern
# Any 4 char sequence which has a pair of two different characters followed by the reverse of that pair
# e.g xaax or raar. The string is not considered valid if the pattern above exist in square brackets.
# For example:
# rttr[mnop]qrst this is valid (rttr outside square brackets).
# efgh[baab]ommo this is invalid (baab is within brackets, ommo outside brackets).
# bbbb[qwer]ereq this is invalid (bbbb is invalid, since the middle two characters should be different.
# irttrj[asdfgh]zxcvbn this is valid (rttr is outside square brackets).
# Sample Input (Please store this in a file)
# ===========
# xdsqxnovprgovwzkus[fmadbfsbqwzzrzrgdg]aeqornszgvbizdm
# itgslvpxoqqakli[arktzcssgkxktejbno]wsgkbwwtbmfnddt[zblrboqsvezcgfmfvcz]iwyhyatqetsreeyhh
# pyxuijrepsmyiacl[rskpebsqdfctoqg]hbwageeiufvcmuk[wfvdhxyzmfgmcphpfnc]aotmbcnntmdltjxuusn
# mfhczaevladdsqawgp[rwabwdnwiytloldf]varesbnjnsdbsmhmsi[tyjtbpzrbfzbwlga]sznkksuymkbyxlykfqg[fyislgfghcbltaft]knrkzaldhauordwfl
# piftqfdhtumcmjmsge[qrsntvxhtfurcgcynx]oyswvuklvtmivlhen[syqhqtijyiduoxb]pdtdrhijqqzvcnl[xivmeqcwyafxvnok]jvlbkrwbgcgzaqms
# pfqiqyscrxhvtrjzt[unmovhoommbcckocp]ziwuhtfghcqhzeysdw[zmhlfonldrgkbimft]nnlbctvfpbcoqzw[zivyewjzuuvvasybded]mznpvozhzsvkdedqu
# adncdhtushtvtfcbez[rvaycmplefdvbrchc]vtviiplkpfhsyhwzz[pdpnsseaizogzvtkcq]piorguaivfpummlo
# cdgyiakhcpbibtdwm[dqmibwtfswjlfxvwe]jghsohdnnowueerunt[stsuvrwswspkgom]mmyifoverwkyjqfofhd
# luqpeubugunvgzdqk[jfnihalscclrffkxqz]wvzpvmpfiehevybbgpg[esjuempbtmfmwwmqa]rhflhjrqjbbsadjnyc
# yqdhleetfcqhdiib[eceprgdrrsmbarxdtbq]hdayiijoaaeumfwcdj
# cqqvoxzdokmgiwgcks[jqzwdkyjpbdchlt]phkfcoalnhoxnczrru
# uxpvoytxfazjjhi[qogwhtzmwxvjwxreuz]zduoybbzxigwggwu[lamifchqqwbphhsqnf]qrjdjwtnhsjqftnqsk[bsqinwypsnnvougrs]wfmhtjkysqffllakru
# jfuokpqkhmnvixa[fxfcqxfxbmhazuspg]eqfpfndvqnxluairk
# rvvyvofaygynnetjtry[kegzdkleyezldyeyn]erioueyndgksxetku[tsarhnyrbaubgmteiw]lbcsksdiqqdacutvc
#! /usr/bin/env python
from typing import List
import fileinput
def is_phrase_valid(s: str) -> bool:
return False if len(s) < 4 else (s[3] == s[0] and s[2] == s[1] and s[0] != s[1]) or is_phrase_valid(s[1:])
def split_into_sub_strings(s: str) -> List[str]:
return s.strip().replace("]", "[").split("[")
def is_all_true(x: List[bool]) -> bool:
return True if len(x) == 0 else x[0] and is_all_true(x[1:])
def is_one_true(x: List[bool]) -> bool:
return False if len(x) == 0 else x[0] or is_one_true(x[1:])
def is_line_valid(s: str) -> bool:
phrases = split_into_sub_strings(s)
validity_of_phrases_outside_of_bracket = [is_phrase_valid(p) for i, p in enumerate(phrases) if i % 2 == 0]
validity_of_phrases_inside_of_bracket = [not is_phrase_valid(p) for i, p in enumerate(phrases) if i % 2 != 0]
return is_one_true(validity_of_phrases_outside_of_bracket) and is_all_true(validity_of_phrases_inside_of_bracket)
if __name__ == "__main__":
for line in fileinput.input():
if is_line_valid(line):
print(line.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment