Skip to content

Instantly share code, notes, and snippets.

@james-gibson
Last active February 5, 2026 07:26
Show Gist options
  • Select an option

  • Save james-gibson/df827e73c5209b70eb0bf7c504dc4e96 to your computer and use it in GitHub Desktop.

Select an option

Save james-gibson/df827e73c5209b70eb0bf7c504dc4e96 to your computer and use it in GitHub Desktop.

Agent Skill Examples: Counting R's in "Strawberry"

1. Direct Answer Skill

---
id: skill_count_r_direct
title: Count R's in Strawberry - Direct Answer
tags: [skill, example, text-analysis]
created: 2026-02-05
updated: 2026-02-05
---

# Count R's in Strawberry - Direct Answer

## Skill Description
Counts the letter 'r' in the word "strawberry".

## Implementation
Direct character counting by inspection.

## Answer
The word "strawberry" contains **3** letter r's:
- st**r**awbe**rr**y
- Position 3: r
- Position 7: r
- Position 8: r

## Use Case
Quick reference for letter frequency questions.

2. Grep-Based Skill

---
id: skill_count_r_grep
title: Count R's in Strawberry - Grep Method
tags: [skill, example, bash, grep]
created: 2026-02-05
updated: 2026-02-05
---

# Count R's in Strawberry - Grep Method

## Skill Description
Uses grep to count occurrences of 'r' in "strawberry".

## Implementation

```bash
#!/bin/bash
# Count r's using grep with -o flag (only matching)
echo "strawberry" | grep -o 'r' | wc -l

Output

3

Explanation

  • grep -o 'r' outputs each match on a separate line
  • wc -l counts the number of lines
  • Case-sensitive by default (use grep -io for case-insensitive)

Alternative Methods

# Case-insensitive version
echo "strawberry" | grep -io 'r' | wc -l

# Using sed
echo "strawberry" | sed 's/[^rR]//g' | wc -c

# Using tr
echo "strawberry" | tr -cd 'rR' | wc -c

## 3. Python-Based Skill

```markdown
---
id: skill_count_r_python
title: Count R's in Strawberry - Python Method
tags: [skill, example, python, string-manipulation]
created: 2026-02-05
updated: 2026-02-05
---

# Count R's in Strawberry - Python Method

## Skill Description
Python implementation for counting 'r' characters in "strawberry".

## Implementation

```python
def count_letter(word: str, letter: str, case_sensitive: bool = True) -> int:
    """Count occurrences of a letter in a word."""
    if not case_sensitive:
        word = word.lower()
        letter = letter.lower()
    return word.count(letter)

# Example usage
word = "strawberry"
letter = "r"
count = count_letter(word, letter)
print(f"The word '{word}' contains {count} letter '{letter}'s")

# Alternative: using list comprehension
count_alt = sum(1 for char in word if char.lower() == letter.lower())
print(f"Alternative method: {count_alt}")

# Show positions
positions = [i for i, char in enumerate(word) if char.lower() == letter.lower()]
print(f"Positions: {positions}")

Output

The word 'strawberry' contains 3 letter 'r's
Alternative method: 3
Positions: [2, 6, 7]

Advanced Features

from collections import Counter

def analyze_word(word: str) -> dict:
    """Comprehensive letter frequency analysis."""
    counter = Counter(word.lower())
    return {
        'total_chars': len(word),
        'unique_chars': len(counter),
        'frequencies': dict(counter),
        'r_count': counter.get('r', 0)
    }

result = analyze_word("strawberry")
print(f"Analysis: {result}")
# Output: {'total_chars': 10, 'unique_chars': 7, 
#          'frequencies': {'s': 1, 't': 1, 'r': 3, 'a': 1, 'w': 1, 'b': 1, 'e': 1, 'y': 1}, 
#          'r_count': 3}

## 4. TypeScript-Based Skill

```markdown
---
id: skill_count_r_typescript
title: Count R's in Strawberry - TypeScript Method
tags: [skill, example, typescript, string-manipulation]
created: 2026-02-05
updated: 2026-02-05
---

# Count R's in Strawberry - TypeScript Method

## Skill Description
TypeScript implementation for counting 'r' characters in "strawberry".

## Implementation

```typescript
function countLetter(
  word: string, 
  letter: string, 
  caseSensitive: boolean = true
): number {
  const searchWord = caseSensitive ? word : word.toLowerCase();
  const searchLetter = caseSensitive ? letter : letter.toLowerCase();
  
  return (searchWord.match(new RegExp(searchLetter, 'g')) || []).length;
}

// Example usage
const word = "strawberry";
const letter = "r";
const count = countLetter(word, letter);
console.log(`The word '${word}' contains ${count} letter '${letter}'s`);

// Alternative: using split method
const countAlt = word.split(letter).length - 1;
console.log(`Alternative method: ${countAlt}`);

// Show positions
const positions = [...word].reduce((acc, char, idx) => {
  if (char.toLowerCase() === letter.toLowerCase()) acc.push(idx);
  return acc;
}, [] as number[]);
console.log(`Positions: ${positions}`);

Output

The word 'strawberry' contains 3 letter 'r's
Alternative method: 3
Positions: [2, 6, 7]

Advanced TypeScript Features

interface LetterAnalysis {
  totalChars: number;
  uniqueChars: number;
  frequencies: Record<string, number>;
  rCount: number;
}

function analyzeWord(word: string): LetterAnalysis {
  const lowerWord = word.toLowerCase();
  const frequencies: Record<string, number> = {};
  
  for (const char of lowerWord) {
    frequencies[char] = (frequencies[char] || 0) + 1;
  }
  
  return {
    totalChars: word.length,
    uniqueChars: Object.keys(frequencies).length,
    frequencies,
    rCount: frequencies['r'] || 0
  };
}

const result = analyzeWord("strawberry");
console.log('Analysis:', result);
// Output: {
//   totalChars: 10,
//   uniqueChars: 7,
//   frequencies: { s: 1, t: 1, r: 3, a: 1, w: 1, b: 1, e: 1, y: 1 },
//   rCount: 3
// }

// Functional approach with reduce
const countLetterFunctional = (word: string, letter: string): number =>
  [...word.toLowerCase()].reduce(
    (count, char) => count + (char === letter.toLowerCase() ? 1 : 0),
    0
  );

console.log(`Functional count: ${countLetterFunctional("strawberry", "r")}`);

Type-Safe Version

type Letter = string & { readonly __brand: unique symbol };

function createLetter(char: string): Letter | null {
  if (char.length !== 1 || !/[a-zA-Z]/.test(char)) {
    return null;
  }
  return char as Letter;
}

function countLetterTypeSafe(word: string, letter: Letter): number {
  return [...word.toLowerCase()].filter(
    char => char === letter.toLowerCase()
  ).length;
}

const r = createLetter('r');
if (r) {
  console.log(`Type-safe count: ${countLetterTypeSafe("strawberry", r)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment