Skip to content

Commit de5ba69

Browse files
committed
update tasks
1 parent f8f25d6 commit de5ba69

File tree

40 files changed

+1001
-0
lines changed

40 files changed

+1001
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# Exercise 3-1: Optimized Binary Search
3+
4+
## Task
5+
Modify binary search to perform only one test inside the loop instead of two. Move any necessary checks outside the loop.
6+
7+
## Steps
8+
1. Reduce the number of tests inside the loop.
9+
2. Add required checks outside the loop.
10+
3. Measure and compare execution time with the original.
11+
12+
## Expected Outcome
13+
Analyze whether the optimization improves performance.
14+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Exercise 3-2: Escape and Unescape Functions
3+
4+
## Task
5+
Write a function `escape(s, t)` that converts special characters (e.g., newline, tab) in `t` into visible escape sequences (e.g., `\n`, `\t`) and stores the result in `s`. Use a `switch` statement.
6+
7+
Write another function to perform the reverse operation, converting escape sequences back into their actual characters.
8+
9+
## Steps
10+
1. Implement `escape(s, t)` using a `switch` statement.
11+
2. Implement the reverse function.
12+
3. Test both functions with various input cases.
13+
14+
## Expected Outcome
15+
The functions should correctly transform strings between escaped and unescaped representations.
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# Exercise 3-3: Expand Shorthand Notation
3+
4+
## Task
5+
Write a function `expand(s1, s2)` that expands shorthand notations like `a-z` in `s1` into the full sequence `abc...xyz` in `s2`. The function should handle:
6+
- Both uppercase and lowercase letters.
7+
- Digit ranges like `0-9`.
8+
- Edge cases such as `a-b-c`, `a-z0-9`, and `-a-z`, ensuring leading or trailing `-` is treated literally.
9+
10+
## Steps
11+
1. Parse `s1` and identify valid shorthand ranges.
12+
2. Expand recognized patterns into their full sequences.
13+
3. Handle edge cases correctly.
14+
4. Store the result in `s2` and test with various inputs.
15+
16+
## Expected Outcome
17+
The function should accurately expand shorthand notations while correctly handling edge cases.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Exercise 3-4: Handling the Largest Negative Number in itoa
3+
4+
## Task
5+
In two's complement representation, the standard `itoa` function fails for the most negative number (`-(2^(wordsize-1))`). This happens because negating this value exceeds the range of positive numbers representable in the same number of bits.
6+
7+
Modify `itoa` to correctly handle this edge case on any machine.
8+
9+
## Steps
10+
1. Identify why `itoa` fails for the largest negative number.
11+
2. Implement a fix that works across different word sizes.
12+
3. Ensure correctness by testing with various inputs, including the edge case.
13+
14+
## Expected Outcome
15+
The modified `itoa` function should correctly convert the most negative number to a string representation, regardless of system architecture.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Exercise 3-5: Implementing the itob Function for Base Conversion
3+
4+
## Task
5+
Write the function `itob(n, s, b)` that converts the integer `n` into a base `b` character representation in the string `s`. In particular, `itob(n, s, 16)` should format `s` as a hexadecimal integer in `s`.
6+
7+
## Steps
8+
1. Identify the logic needed to convert an integer `n` into a string representation in a given base `b`.
9+
2. Implement the conversion for bases 2 through 16, mapping numbers to the correct characters (`0-9` for digits and `a-f` for hexadecimal).
10+
3. Handle both positive and negative numbers, ensuring that negative values are properly represented.
11+
4. Reverse the string since the remainder of divisions is stored in reverse order.
12+
5. Add a null terminator at the end of the string.
13+
14+
## Expected Outcome
15+
The function `itob` should correctly convert integers into strings representing the number in any base from 2 to 16, with correct handling of both positive and negative integers.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Exercise 3-6: Implementing itoa with Minimum Field Width
3+
4+
## Task
5+
Write a version of `itoa` that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.
6+
7+
## Steps
8+
1. Modify the `itoa` function to accept three arguments: the integer `n`, the character array `s`, and the minimum field width `width`.
9+
2. Implement the logic to convert the integer `n` into a string representation.
10+
3. Ensure that the string is padded with blank spaces on the left if its length is less than the specified minimum field width `width`.
11+
4. Handle both positive and negative numbers, ensuring that negative values are represented correctly and the padding does not overwrite the negative sign.
12+
5. Ensure that the converted string is null-terminated.
13+
14+
## Expected Outcome
15+
The modified `itoa` function should correctly convert the integer `n` into a string, ensuring that if the converted number is shorter than the specified field width, it is padded with blank spaces on the left.
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Exercise 4-1: Implementing strindex to Find the Rightmost Occurrence
3+
4+
## Task
5+
Write the function `strindex(s, t)` which returns the position of the rightmost occurrence of the string `t` in the string `s`, or `-1` if there is none.
6+
7+
## Steps
8+
1. Implement the function `strindex` that takes two string arguments, `s` and `t`.
9+
2. Search for the rightmost occurrence of string `t` in string `s`.
10+
3. Return the position (index) of the first character of the rightmost occurrence of `t` in `s`. If `t` does not exist in `s`, return `-1`.
11+
4. Ensure that the function works for all valid input strings, including edge cases where `t` is empty or not found.
12+
13+
## Expected Outcome
14+
The function `strindex` should correctly return the index of the rightmost occurrence of `t` in `s`, or `-1` if `t` is not found.
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Exercise 4-10: Revising the Calculator to Use getline Instead of getch/ungetch
3+
4+
## Task
5+
An alternate organization uses `getline` to read an entire input line; this makes `getch` and `ungetch` unnecessary. Revise the calculator to use `getline` for input instead of `getch` and `ungetch`.
6+
7+
## Steps
8+
1. Replace the usage of `getch` and `ungetch` with `getline` to read entire lines of input.
9+
2. Modify the input parsing logic to handle input as a complete line, instead of reading one character at a time.
10+
3. Ensure that `getline` can properly read input of varying lengths, handling large inputs and input with spaces correctly.
11+
4. Update the tokenizer or expression parsing logic to work with entire lines of input rather than processing characters individually.
12+
5. Remove any code related to managing a character buffer (e.g., `buf` and `bufp`) that was previously used with `getch` and `ungetch`.
13+
6. Test the modified calculator to ensure it behaves correctly with this new input handling approach, including handling different input sizes and formats.
14+
15+
## Expected Outcome
16+
The calculator should function correctly with `getline` used to read entire input lines. It should no longer rely on `getch` and `ungetch`, and the logic should be updated to handle complete lines of input. The calculator should properly parse and evaluate expressions from the input lines.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Exercise 4-11: Modifying getop to Avoid Using ungetch
3+
4+
## Task
5+
Modify `getop` so that it doesn't need to use `ungetch`. Use an internal static variable to store the pushed-back character instead.
6+
7+
## Steps
8+
1. Analyze the current implementation of `getop` and identify where it relies on `ungetch` to push characters back.
9+
2. Replace the use of `ungetch` with an internal static variable within `getop`. This static variable will store the last pushed-back character.
10+
3. Implement logic within `getop` to check if the internal static variable has a stored character. If it does, return that character first before reading new input.
11+
4. If the internal static variable is empty, proceed with reading a new character from the input.
12+
5. Ensure that once the internal static variable is used, it is reset to avoid incorrect behavior during future calls to `getop`.
13+
6. Test the modified `getop` to ensure it correctly handles input and pushed-back characters, without requiring `ungetch`.
14+
15+
## Expected Outcome
16+
The modified `getop` function should no longer need to rely on `ungetch` for pushing characters back. Instead, it should use an internal static variable to store and retrieve a pushed-back character. The function should behave correctly, with input being handled efficiently and without errors.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Exercise 4-12: Writing a Recursive Version of itoa
3+
4+
## Task
5+
Adapt the ideas of `printd` to write a recursive version of `itoa`. This version should convert an integer into a string by calling a recursive routine.
6+
7+
## Steps
8+
1. Understand how `itoa` works by converting an integer into a string using iteration. The recursive version will rely on similar principles but will use recursion instead of iteration.
9+
2. Implement a recursive function that converts the integer to a string. The base case for the recursion should handle when the number is zero, and the recursive case should reduce the problem size by dividing the number by 10.
10+
3. In the recursive function, handle the sign of the number by checking if it's negative and appropriately adding the minus sign when necessary.
11+
4. Build the string from the least significant digit to the most significant one. This can be done by recursively processing the digits in reverse order.
12+
5. Once the recursive function is complete, ensure that the string is properly null-terminated and handles edge cases such as zero and negative numbers.
13+
14+
## Expected Outcome
15+
The recursive `itoa` function should correctly convert an integer into a string, with each recursive call processing one digit at a time. The function should properly handle both positive and negative integers and return a correctly formatted string

0 commit comments

Comments
 (0)