Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bob exercise #237

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@
"floating_point_numbers"
]
},
{
"slug": "bob",
"name": "Bob",
"uuid": "6d8927ca-2aaa-4af4-bb38-c307b2105e41",
"practices": [],
"prerequisites": [],
"difficulty": 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentionally marked easier than, say, resistor duo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional. Now increased to 3, to match resistor-color-duo.

I think resistor-color difficulty rating was also too low on this track (#241).

},
{
"slug": "hamming",
"name": "Hamming",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/bob/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.

Bob only ever answers one of five things:

- **"Sure."**
This is his response if you ask him a question, such as "How are you?"
The convention used for questions is that it ends with a question mark.
- **"Whoa, chill out!"**
This is his answer if you YELL AT HIM.
The convention used for yelling is ALL CAPITAL LETTERS.
- **"Calm down, I know what I'm doing!"**
This is what he says if you yell a question at him.
- **"Fine. Be that way!"**
This is how he responds to silence.
The convention used for silence is nothing, or various combinations of whitespace characters.
- **"Whatever."**
This is what he answers to anything else.
10 changes: 10 additions & 0 deletions exercises/practice/bob/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Introduction

Bob is a [lackadaisical][] teenager.
He likes to think that he's very cool.
And he definitely doesn't get excited about things.
That wouldn't be cool.

When people talk to him, his responses are pretty limited.

[lackadaisical]: https://www.collinsdictionary.com/dictionary/english/lackadaisical
2 changes: 2 additions & 0 deletions exercises/practice/bob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
tests
19 changes: 19 additions & 0 deletions exercises/practice/bob/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"bob.asm"
],
"test": [
"bob_test.c"
],
"example": [
".meta/example.asm"
]
},
"blurb": "Bob is a lackadaisical teenager. In conversation, his responses are very limited.",
"source": "Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial.",
"source_url": "https://pine.fm/LearnToProgram/?Chapter=06"
}
87 changes: 87 additions & 0 deletions exercises/practice/bob/.meta/example.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
default rel

section .rodata
sure: db "Sure.", 0
whoa: db "Whoa, chill out!", 0
calm: db "Calm down, I know what I'm doing!", 0
fine: db "Fine. Be that way!", 0
whatever: db "Whatever.", 0

section .text
global response
response:
cld
mov rsi, rdi
mov rcx, 1
xor r8, r8 ; nonempty?
xor r9, r9 ; ends with a question mark?
xor r10, r10 ; contains upper-case?
xor r11, r11 ; contains lower-case?
jmp .read

.parse:
cmp al, ' '
jbe .read

mov r8, rcx ; nonempty

xor r9, r9
cmp al, '?' ; ends with a question mark?
cmove r9, rcx

sub al, 'A'
cmp al, 26 ; contains upper-case?
cmovb r10, rcx

sub al, 32
cmp al, 26 ; contains lower-case?
cmovb r11, rcx

.read:
lodsb
test al, al
jnz .parse

test r8, r8
jz .return_fine

; Does the input contain upper-case and no lower-case?
sub r10, r11
cmp r10, rcx
je .yell

test r9, r9
jz .return_whatever

.return_sure:
; This is his response if you ask him a question, such as "How are you?"
lea rax, [sure]
ret

.yell:
test r9, r9
jnz .return_calm

.return_whoa:
; This is his answer if you YELL AT HIM.
lea rax, [whoa]
ret

.return_calm:
; This is what he says if you yell a question at him.
lea rax, [calm]
ret

.return_fine:
; This is how he responds to silence.
lea rax, [fine]
ret

.return_whatever:
; This is what he answers to anything else.
lea rax, [whatever]
ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
85 changes: 85 additions & 0 deletions exercises/practice/bob/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e162fead-606f-437a-a166-d051915cea8e]
description = "stating something"

[73a966dc-8017-47d6-bb32-cf07d1a5fcd9]
description = "shouting"

[d6c98afd-df35-4806-b55e-2c457c3ab748]
description = "shouting gibberish"

[8a2e771d-d6f1-4e3f-b6c6-b41495556e37]
description = "asking a question"

[81080c62-4e4d-4066-b30a-48d8d76920d9]
description = "asking a numeric question"

[2a02716d-685b-4e2e-a804-2adaf281c01e]
description = "asking gibberish"

[c02f9179-ab16-4aa7-a8dc-940145c385f7]
description = "talking forcefully"

[153c0e25-9bb5-4ec5-966e-598463658bcd]
description = "using acronyms in regular speech"

[a5193c61-4a92-4f68-93e2-f554eb385ec6]
description = "forceful question"

[a20e0c54-2224-4dde-8b10-bd2cdd4f61bc]
description = "shouting numbers"

[f7bc4b92-bdff-421e-a238-ae97f230ccac]
description = "no letters"

[bb0011c5-cd52-4a5b-8bfb-a87b6283b0e2]
description = "question with no letters"

[496143c8-1c31-4c01-8a08-88427af85c66]
description = "shouting with special characters"

[e6793c1c-43bd-4b8d-bc11-499aea73925f]
description = "shouting with no exclamation mark"

[aa8097cc-c548-4951-8856-14a404dd236a]
description = "statement containing question mark"

[9bfc677d-ea3a-45f2-be44-35bc8fa3753e]
description = "non-letters with question"

[8608c508-f7de-4b17-985b-811878b3cf45]
description = "prattling on"

[bc39f7c6-f543-41be-9a43-fd1c2f753fc0]
description = "silence"

[d6c47565-372b-4b09-b1dd-c40552b8378b]
description = "prolonged silence"

[4428f28d-4100-4d85-a902-e5a78cb0ecd3]
description = "alternate silence"

[66953780-165b-4e7e-8ce3-4bcb80b6385a]
description = "multiple line question"

[5371ef75-d9ea-4103-bcfa-2da973ddec1b]
description = "starting with whitespace"

[05b304d6-f83b-46e7-81e0-4cd3ca647900]
description = "ending with whitespace"

[72bd5ad3-9b2f-4931-a988-dce1f5771de2]
description = "other whitespace"

[12983553-8601-46a8-92fa-fcaa3bc4a2a0]
description = "non-question ending with whitespace"
46 changes: 46 additions & 0 deletions exercises/practice/bob/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
AS = nasm

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =
ASFLAGS = -g -F dwarf -Werror

ifeq ($(shell uname -s),Darwin)
ifeq ($(shell sysctl -n hw.optional.arm64 2>/dev/null),1)
ALL_CFLAGS = -target x86_64-apple-darwin
endif
ALL_LDFLAGS = -Wl,-pie
ALL_ASFLAGS = -f macho64 --prefix _
else
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
ALL_ASFLAGS = -f elf64
endif

ALL_CFLAGS += -std=c99 -fPIE -m64 $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)
ALL_ASFLAGS += $(ASFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.asm,%.o,$(wildcard *.asm))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
@./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.asm
@$(AS) $(ALL_ASFLAGS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
11 changes: 11 additions & 0 deletions exercises/practice/bob/bob.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default rel

section .text
global response
response:
; Provide your implementation here
ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
Loading