Algorithms Problem Solving: to Lower case
Tags: algorithms, python, computerscience, interview
Published: 2020-06-09 11:12:17

---
title: Algorithms Problem Solving: to Lower case
published: true
description: Solving algorithms problems with TK
tags: algorithms, python, computerscience, interview
cover_image: https://raw.githubusercontent.com/leandrotk/tk/master/series/algorithms-problem-solving/assets/father-and-daughter.jpg
canonical_url: https://leandrotk.github.io/tk/series/algorithms-problem-solving/to-lower-case
series: Algorithms Problem Solving Series
---

This post is part of the [Algorithms Problem Solving](https://leandrotk.github.io/tk/series/algorithms-problem-solving/index.html) series.

## Problem description

This is the [To Lower](https://leetcode.com/problems/to-lower-case/) [Case](https://leetcode.com/problems/to-lower-case/https://leetcode.com/problems/to-lower-case/) problem. The description looks like this:

Implement function ToLowerCase() that has a string parameter str,
and returns the same string in lowercase.

## Examples

```python
Example 1:
Input: "Hello"
Output: "hello"

Example 2:
Input: "here"
Output: "here"

Example 3:
Input: "LOVELY"
Output: "lovely"
```

## Solution

My solution to this problem was to iterate through the entire string, and for each character, I append its lower case version. To verify if it needs to transform the character to lower case, I simply get the integer representation of the Unicode character by using the `ord` function. If it is between 65 and 90, both inclusive, the character is capitalized, so I need to transform it to lower case.

To transform to lower case, I just need to:

- Transform to the Unicode integer
- Sum 32
- Transform back to the character representation of the Unicode

After the process of each character, I have my list of lower case characters. Then, I just need to join all the characters to get my final string back.

```python
def lower_case_char(char):
    if ord(char) >= 65 and ord(char) <= 90:
        return chr(ord(char) + 32)

    return char

def lower_case(str):
    list_of_chars = []

    for char in str:
        list_of_chars.append(lower_case_char(char))

    return ''.join(list_of_chars)
```

## Resources

- [Learning Python: From Zero to Hero](https://leandrotk.github.io/tk/2017/09/learning-python-from-zero-to-hero/index.html)
- [Algorithms Problem Solving Series](https://leandrotk.github.io/tk/series/algorithms-problem-solving/index.html)
- [Stack Data Structure](https://leandrotk.github.io/tk/2020/01/stack-data-structure/index.html)
- [Queue Data Structure](https://leandrotk.github.io/tk/2020/01/queue-data-structure/index.html)
- [Linked List](https://leandrotk.github.io/tk/2020/02/linked-list/index.html)
- [Tree Data Structure](https://leandrotk.github.io/tk/2020/02/tree-data-structure/index.html)
- [Learn Object-Oriented Programming in Python](https://www.educative.io/courses/learn-object-oriented-programming-in-python?aff=x8bV)
- [Data Structures in Python: An Interview Refresher](https://www.educative.io/courses/data-structures-in-python-an-interview-refresher?aff=x8bV)
- [Data Structures and Algorithms in Python](https://www.educative.io/courses/ds-and-algorithms-in-python?aff=x8bV)
- [Data Structures for Coding Interviews in Python](https://www.educative.io/courses/data-structures-coding-interviews-python?aff=x8bV)
- [One Month Python Course](https://mbsy.co/lG6tv)
- [Big-O Notation For Coding Interviews and Beyond](https://www.educative.io/courses/big-o-notation-for-interviews-and-beyond?aff=x8bV)
- [Learn Python from Scratch](https://www.educative.io/courses/learn-python-from-scratch?aff=x8bV)
- [Learn Object-Oriented Programming in Python](https://www.educative.io/courses/learn-object-oriented-programming-in-python?aff=x8bV)
- [Data Structures in Python: An Interview Refresher](https://www.educative.io/courses/data-structures-in-python-an-interview-refresher?aff=x8bV)
- [Data Structures and Algorithms in Python](https://www.educative.io/courses/ds-and-algorithms-in-python?aff=x8bV)
- [Data Structures for Coding Interviews in Python](https://www.educative.io/courses/data-structures-coding-interviews-python?aff=x8bV)
- [One Month Python Course](https://mbsy.co/lG6tv)