September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,13 +0,0 @@
|
|||
FileSelectFile, filename, , %A_ScriptDir%, Select a Mad Libs template, *.txt
|
||||
If ErrorLevel
|
||||
ExitApp ; the user canceled the file selection
|
||||
FileRead, contents, %filename%
|
||||
pos := match := 0
|
||||
While pos := RegExMatch(contents, "<[^>]+>", match, pos+strLen(match))
|
||||
{
|
||||
InputBox, repl, Mad Libs, Enter a replacement for %match%:
|
||||
If ErrorLevel ; cancelled inputbox
|
||||
ExitApp
|
||||
StringReplace, contents, contents, %match%, %repl%, All
|
||||
}
|
||||
MsgBox % contents
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string story, input;
|
||||
|
||||
//Loop
|
||||
while(true)
|
||||
{
|
||||
//Get a line from the user
|
||||
getline(cin, input);
|
||||
|
||||
//If it's blank, break this loop
|
||||
if(input == "\r")
|
||||
break;
|
||||
|
||||
//Add the line to the story
|
||||
story += input;
|
||||
}
|
||||
|
||||
//While there is a '<' in the story
|
||||
int begin;
|
||||
while((begin = story.find("<")) != string::npos)
|
||||
{
|
||||
//Get the category from between '<' and '>'
|
||||
int end = story.find(">");
|
||||
string cat = story.substr(begin + 1, end - begin - 1);
|
||||
|
||||
//Ask the user for a replacement
|
||||
cout << "Give me a " << cat << ": ";
|
||||
cin >> input;
|
||||
|
||||
//While there's a matching category
|
||||
//in the story
|
||||
while((begin = story.find("<" + cat + ">")) != string::npos)
|
||||
{
|
||||
//Replace it with the user's replacement
|
||||
story.replace(begin, cat.length()+2, input);
|
||||
}
|
||||
}
|
||||
|
||||
//Output the final story
|
||||
cout << endl << story;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
::Save this as MADLIBS.BAT
|
||||
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
%== Check if there is no arguments ==%
|
||||
if "%~1"=="" (
|
||||
echo.
|
||||
echo.[Mad Libs - Batch File Implementation]
|
||||
echo.
|
||||
echo.Usage: MADLIBS [file]
|
||||
echo.
|
||||
exit /b 1
|
||||
)
|
||||
if not exist "%~f1" (echo.File not found.&exit /b 1)
|
||||
|
||||
echo.
|
||||
%== Read the text file ==%
|
||||
echo.[Mad Libs - Batch File Implementation]
|
||||
echo.
|
||||
for /f "tokens=* eol=_" %%A in (%~sf1) do (
|
||||
set /a cnt+=1
|
||||
set "line!cnt!=%%A"
|
||||
)
|
||||
%== User input the missing parts ==%
|
||||
for /l %%. in (1,1,!cnt!) do (
|
||||
call :proc_line "%%."
|
||||
)
|
||||
%== Display the edited story... ==%
|
||||
echo.
|
||||
echo.The Story:
|
||||
echo.
|
||||
for /l %%? in (1,1,!cnt!) do (
|
||||
echo. !line%%?!
|
||||
)
|
||||
echo.
|
||||
exit /b 0
|
||||
|
||||
%== The main processor of the story ==%
|
||||
:proc_line
|
||||
set "str=!line%~1!"
|
||||
:loop
|
||||
if "!str!"=="" goto :EOF
|
||||
for /f "tokens=1,* delims=<" %%M in ("!str!") do (
|
||||
for /f "tokens=1,* delims=>" %%X in ("%%M") do (
|
||||
if not "%%M"=="%%X" (
|
||||
set "temp_var=%%X"
|
||||
if not "!temp_var: =!"=="" (
|
||||
set "input="
|
||||
set /p "input=Enter a value for [%%X]?"
|
||||
call :subst_input
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
set "str=!line%~1!"
|
||||
for /f "tokens=1,* delims=<" %%M in ("!str!") do (set str=%%N)
|
||||
goto loop
|
||||
|
||||
%== This Substitutes the input to the blank ==%
|
||||
:subst_input
|
||||
set "chk_brack=!input:>=!"
|
||||
set "chk_brack=!chk_brack:<=!"
|
||||
set "chk_brack=!chk_brack:%%=!"
|
||||
for /l %%. in (1,1,!cnt!) do (
|
||||
if "!line%%.: =!"==" =" set "line%%.= "
|
||||
if "!chk_brack!"=="!input!" (
|
||||
call set "line%%.=%%line%%.:<%%X>=!input!%%"
|
||||
) else (set "line%%.=!line%%.:<%%X>=!")
|
||||
)
|
||||
goto :EOF
|
||||
|
|
@ -1,145 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define err(...) fprintf(stderr, ## __VA_ARGS__), exit(1)
|
||||
|
||||
/* We create a dynamic string with a few functions which make modifying
|
||||
* the string and growing a bit easier */
|
||||
typedef struct {
|
||||
char *data;
|
||||
size_t alloc;
|
||||
size_t length;
|
||||
} dstr;
|
||||
|
||||
inline int dstr_space(dstr *s, size_t grow_amount)
|
||||
{
|
||||
return s->length + grow_amount < s->alloc;
|
||||
}
|
||||
|
||||
int dstr_grow(dstr *s)
|
||||
{
|
||||
s->alloc *= 2;
|
||||
char *attempt = realloc(s->data, s->alloc);
|
||||
|
||||
if (!attempt) return 0;
|
||||
else s->data = attempt;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
dstr* dstr_init(const size_t to_allocate)
|
||||
{
|
||||
dstr *s = malloc(sizeof(dstr));
|
||||
if (!s) goto failure;
|
||||
|
||||
s->length = 0;
|
||||
s->alloc = to_allocate;
|
||||
s->data = malloc(s->alloc);
|
||||
|
||||
if (!s->data) goto failure;
|
||||
|
||||
return s;
|
||||
|
||||
failure:
|
||||
if (s->data) free(s->data);
|
||||
if (s) free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void dstr_delete(dstr *s)
|
||||
{
|
||||
if (s->data) free(s->data);
|
||||
if (s) free(s);
|
||||
}
|
||||
|
||||
dstr* readinput(FILE *fd)
|
||||
{
|
||||
static const size_t buffer_size = 4096;
|
||||
char buffer[buffer_size];
|
||||
|
||||
dstr *s = dstr_init(buffer_size);
|
||||
if (!s) goto failure;
|
||||
|
||||
while (fgets(buffer, buffer_size, fd)) {
|
||||
while (!dstr_space(s, buffer_size))
|
||||
if (!dstr_grow(s)) goto failure;
|
||||
|
||||
strncpy(s->data + s->length, buffer, buffer_size);
|
||||
s->length += strlen(buffer);
|
||||
}
|
||||
|
||||
return s;
|
||||
|
||||
failure:
|
||||
dstr_delete(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void dstr_replace_all(dstr *story, const char *replace, const char *insert)
|
||||
{
|
||||
const size_t replace_l = strlen(replace);
|
||||
const size_t insert_l = strlen(insert);
|
||||
char *start = story->data;
|
||||
|
||||
while ((start = strstr(start, replace))) {
|
||||
if (!dstr_space(story, insert_l - replace_l))
|
||||
if (!dstr_grow(story)) err("Failed to allocate memory");
|
||||
|
||||
if (insert_l != replace_l) {
|
||||
memmove(start + insert_l, start + replace_l, story->length -
|
||||
(start + replace_l - story->data));
|
||||
|
||||
/* Remember to null terminate the data so we can utilize it
|
||||
* as we normally would */
|
||||
story->length += insert_l - replace_l;
|
||||
story->data[story->length] = 0;
|
||||
}
|
||||
|
||||
memmove(start, insert, insert_l);
|
||||
}
|
||||
}
|
||||
|
||||
void madlibs(dstr *story)
|
||||
{
|
||||
static const size_t buffer_size = 128;
|
||||
char insert[buffer_size];
|
||||
char replace[buffer_size];
|
||||
|
||||
char *start,
|
||||
*end = story->data;
|
||||
|
||||
while (start = strchr(end, '<')) {
|
||||
if (!(end = strchr(start, '>'))) err("Malformed brackets in input");
|
||||
|
||||
/* One extra for current char and another for nul byte */
|
||||
strncpy(replace, start, end - start + 1);
|
||||
replace[end - start + 1] = '\0';
|
||||
|
||||
printf("Enter value for field %s: ", replace);
|
||||
|
||||
fgets(insert, buffer_size, stdin);
|
||||
const size_t il = strlen(insert) - 1;
|
||||
if (insert[il] == '\n')
|
||||
insert[il] = '\0';
|
||||
|
||||
dstr_replace_all(story, replace, insert);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) return 0;
|
||||
|
||||
FILE *fd = fopen(argv[1], "r");
|
||||
if (!fd) err("Could not open file: '%s\n", argv[1]);
|
||||
|
||||
dstr *story = readinput(fd); fclose(fd);
|
||||
if (!story) err("Failed to allocate memory");
|
||||
|
||||
madlibs(story);
|
||||
printf("%s\n", story->data);
|
||||
dstr_delete(story);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string story, input;
|
||||
|
||||
//Loop
|
||||
while(true)
|
||||
{
|
||||
//Get a line from the user
|
||||
getline(cin, input);
|
||||
|
||||
//If it's blank, break this loop
|
||||
if(input == "\r")
|
||||
break;
|
||||
|
||||
//Add the line to the story
|
||||
story += input;
|
||||
}
|
||||
|
||||
//While there is a '<' in the story
|
||||
int begin;
|
||||
while((begin = story.find("<")) != string::npos)
|
||||
{
|
||||
//Get the category from between '<' and '>'
|
||||
int end = story.find(">");
|
||||
string cat = story.substr(begin + 1, end - begin - 1);
|
||||
|
||||
//Ask the user for a replacement
|
||||
cout << "Give me a " << cat << ": ";
|
||||
cin >> input;
|
||||
|
||||
//While there's a matching category
|
||||
//in the story
|
||||
while((begin = story.find("<" + cat + ">")) != string::npos)
|
||||
{
|
||||
//Replace it with the user's replacement
|
||||
story.replace(begin, cat.length()+2, input);
|
||||
}
|
||||
}
|
||||
|
||||
//Output the final story
|
||||
cout << endl << story;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,49 +1,52 @@
|
|||
import System.IO
|
||||
import System.Environment
|
||||
import qualified Data.Map as M
|
||||
import System.IO (stdout, hFlush)
|
||||
|
||||
import System.Environment (getArgs)
|
||||
|
||||
import qualified Data.Map as M (Map, lookup, insert, empty)
|
||||
|
||||
getLines :: IO [String]
|
||||
getLines = getLines' [] >>= return . reverse
|
||||
where
|
||||
getLines' xs = do
|
||||
line <- getLine
|
||||
case line of
|
||||
[] -> return xs
|
||||
_ -> getLines' $ line:xs
|
||||
getLines = reverse <$> getLines_ []
|
||||
where
|
||||
getLines_ xs = do
|
||||
line <- getLine
|
||||
case line of
|
||||
[] -> return xs
|
||||
_ -> getLines_ $ line : xs
|
||||
|
||||
prompt :: String -> IO String
|
||||
prompt p = putStr p >> hFlush stdout >> getLine
|
||||
|
||||
getKeyword :: String -> Maybe String
|
||||
getKeyword ('<':xs) = getKeyword' xs []
|
||||
where
|
||||
getKeyword' [] _ = Nothing
|
||||
getKeyword' (x:'>':_) acc = Just $ '<' : (reverse $ '>':x:acc)
|
||||
getKeyword' (x:xs) acc = getKeyword' xs $ x:acc
|
||||
getKeyword _ = Nothing
|
||||
getKeyword ('<':xs) = getKeyword_ xs []
|
||||
where
|
||||
getKeyword_ [] _ = Nothing
|
||||
getKeyword_ (x:'>':_) acc = Just $ '<' : reverse ('>' : x : acc)
|
||||
getKeyword_ (x:xs) acc = getKeyword_ xs $ x : acc
|
||||
getKeyword _ = Nothing
|
||||
|
||||
parseText :: String -> M.Map String String -> IO String
|
||||
parseText [] _ = return []
|
||||
parseText line@(l:lx) keywords = do
|
||||
parseText [] _ = return []
|
||||
parseText line@(l:lx) keywords =
|
||||
case getKeyword line of
|
||||
Nothing -> parseText lx keywords >>= return . (l:)
|
||||
Nothing -> (l :) <$> parseText lx keywords
|
||||
Just keyword -> do
|
||||
let rest = drop (length keyword) line
|
||||
case M.lookup keyword keywords of
|
||||
Nothing -> do
|
||||
newword <- prompt $ "Enter a word for " ++ keyword ++ ": "
|
||||
rest' <- parseText rest $ M.insert keyword newword keywords
|
||||
return $ newword ++ rest'
|
||||
Nothing -> do
|
||||
newword <- prompt $ "Enter a word for " ++ keyword ++ ": "
|
||||
rest_ <- parseText rest $ M.insert keyword newword keywords
|
||||
return $ newword ++ rest_
|
||||
Just knownword -> do
|
||||
rest' <- parseText rest keywords
|
||||
return $ knownword ++ rest'
|
||||
rest_ <- parseText rest keywords
|
||||
return $ knownword ++ rest_
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
nlines <- case args of
|
||||
[] -> getLines >>= return . unlines
|
||||
arg:_ -> readFile arg
|
||||
nlines' <- parseText nlines M.empty
|
||||
args <- getArgs
|
||||
nlines <-
|
||||
case args of
|
||||
[] -> unlines <$> getLines
|
||||
arg:_ -> readFile arg
|
||||
nlines_ <- parseText nlines M.empty
|
||||
putStrLn ""
|
||||
putStrLn nlines'
|
||||
putStrLn nlines_
|
||||
|
|
|
|||
22
Task/Mad-Libs/Kotlin/mad-libs.kotlin
Normal file
22
Task/Mad-Libs/Kotlin/mad-libs.kotlin
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("Please enter a multi-line story template terminated by a blank line\n")
|
||||
val sb = StringBuilder()
|
||||
while (true) {
|
||||
val line = readLine()!!
|
||||
if (line.isEmpty()) break
|
||||
sb.append("$line\n") // preserve line breaks
|
||||
}
|
||||
var story = sb.toString()
|
||||
// identify blanks
|
||||
val r = Regex("<.*?>")
|
||||
val blanks = r.findAll(story).map { it.value }.distinct()
|
||||
println("Please enter your replacements for the following 'blanks' in the story:")
|
||||
for (blank in blanks) {
|
||||
print("${blank.drop(1).dropLast(1)} : ")
|
||||
val repl = readLine()!!
|
||||
story = story.replace(blank, repl)
|
||||
}
|
||||
println("\n$story")
|
||||
}
|
||||
|
|
@ -1,7 +1,4 @@
|
|||
t: {<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home.}
|
||||
view layout [a: area wrap t btn "Done" [x: a/text unview]]
|
||||
w: copy []
|
||||
parse x [any [to "<" copy brackets thru ">" (append w brackets)] to end]
|
||||
w: unique w
|
||||
foreach i w [replace/all x i ask join i ": "]
|
||||
alert x
|
||||
parse x [any [to "<" copy b thru ">" (append w: [] b)] to end]
|
||||
foreach i unique w [replace/all x i ask join i ": "] alert x
|
||||
|
|
|
|||
9
Task/Mad-Libs/Zkl/mad-libs.zkl
Normal file
9
Task/Mad-Libs/Zkl/mad-libs.zkl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
story,line,re:=Data(),"",RegExp("(<[^>]+>)");
|
||||
do{ line=ask("Story: "); story.write(line,"\n") }while(line);
|
||||
while(re.search(story,True)){
|
||||
z,ml,N:=re.matched,z[1],z[0][1]; // z=( (0,6),"<name>" )
|
||||
s:=ask("Text to replace ",ml," with: ");
|
||||
while(Void!=(n:=story.find(ml))){ story[n,N]=s } // replace all <names>s
|
||||
}
|
||||
println("-----------------");
|
||||
story.text.print();
|
||||
Loading…
Add table
Add a link
Reference in a new issue