Skip to content

Commit

Permalink
Add 'math', 'roll', and 'sum' programs
Browse files Browse the repository at this point in the history
  • Loading branch information
za3k committed Feb 24, 2021
1 parent 691e3c6 commit 74e769d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ lwjgl-fix
---
On Arch Linux, fix lwjgl.jar in minecraft

math
---
CLI calculator. Outputs the result of simple math expresisons. Example:

math 2+2

Outputs:

2+2 = 4

markov
---
Markov-chain input on a per-word basis
Expand Down Expand Up @@ -292,6 +302,18 @@ retry
---
Retry a command 5 times or until it succeeds

roll
---
Rolls D&D dice.

Usage:

roll "d6 + 4"

Output:

1d6+4 = 8

rtmux
---

Expand Down Expand Up @@ -346,6 +368,10 @@ static-ip
---
Set up a static ip on linux

sum
---
Adds a list of numbers, one per line.

tag_images
---
Tag images in a directory interactively, using 'feh'. One button resizes, another tags the image using an interactive program. I use this for processing scanned images.
Expand Down
4 changes: 4 additions & 0 deletions math
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python
import sys
s = "".join(sys.argv[1:])
print(s, "=", eval(s))
29 changes: 29 additions & 0 deletions roll
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
import random, re, sys
s = "".join(sys.argv[1:]).replace(" ", "")

REGEX = "(\d*)d(\d+)([-+]\d+)?"
m = re.match(REGEX, s)
if m:
dice, sides, modifier = m.group(1), m.group(2), m.group(3)
if dice:
dice = int(dice)
else:
dice = 1
sides = int(sides)
if modifier and modifier.startswith("+"):
modifier = int(modifier[1:])
elif modifier and modifier.startswith("-"):
modifier = int(modifier)
else:
modifier = 0
result = modifier
for i in range(sides):
result += random.randint(1, sides)
if modifier >= 0:
s = "{}d{}+{}".format(dice, sides, modifier)
else:
s = "{}d{}{}".format(dice, sides, modifier)
print(s, "=", result)
else:
print(s, "INVALID")
2 changes: 2 additions & 0 deletions sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
paste -sd+ - | bc

0 comments on commit 74e769d

Please sign in to comment.