Skip to content

Commit

Permalink
Add branchless.py
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus committed Jan 31, 2024
1 parent 44cb73a commit d96d769
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions branchless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Min = lambda a,b: a * (a < b) + b * (b <= a)
Max = lambda a,b: a * (a > b) + b * (b >= a)

Upper = lambda s: "".join(chr(ord(s[i]) * int(not (s[i] >= 'a' and s[i] <='z')) + (ord(s[i]) - 32) * (s[i] >= 'a' and s[i] <= 'z')) for i in range(0,len(s)))
Lower = lambda s: "".join(chr(ord(s[i]) * int(not (s[i] >= 'A' and s[i] <= 'Z')) + (ord(s[i]) + 32) * (s[i] >= 'A' and s[i] <= 'Z')) for i in range(0,len(s)))

Upper2 = lambda s: "".join(chr(ord(s[i]) - 32 * (s[i] >= 'a' and s[i] <= 'z' )) for i in range(0,len(s)))
Lower2 = lambda s: "".join(chr(ord(s[i]) + 32 * (s[i] >= 'A' and s[i] <= 'Z' )) for i in range(0,len(s)))


def test():
print(Min(3,10))
print(Max(3,10))
print(Upper("dario"))
print(Upper2("dario"))
print(Lower("DARIO"))
print(Lower2("DARIO"))


test()

0 comments on commit d96d769

Please sign in to comment.