# How to use these kata **kata** is a Japanese word meaning 'form'. In martial arts like karate, _kata_ are training exercises. Software engineers have adapted the idea into coding challenges. We have written some geocomputing-flavoured _kata_ for you to try. _Kata_ also happens to mean 'down' in Greek (e.g. as in 'katabatic wind'), so it's especially appropriate for geoscience :) Use the same `geocomp` environment we set up in the class. (If you need to install the `requests` library, you can do that with `pip install requests`.) ## Getting an exercise To receive the challenge, get your input data, and give your responses, you will be communicating with a [web API](https://en.wikipedia.org/wiki/Web_API). The API is being served at the URL `https://kata.scienxlab.org` We can read this week's question from the **challenge** 'endpoint' at `https://kata.scienxlab.org/challenge`. This is expecting you to ask for a resource, which is the challenge name. The first challenge is called **sequence** so for week one, we'll go to `https://kata.scienxlab.org/challenge/sequence` (note the name at the end). You can paste that URL into a browser to read the challange, but we can do it right from Python: import requests url = 'https://kata.scienxlab.org/challenge/sequence' # <--- In week 2, you'll change the name r = requests.get(url) r.status_code What comes back is a string: r.text The text is written in [Markdown](https://daringfireball.net/projects/markdown/syntax), a markup language for formatting plain text. We can use IPython's display module to render it: from IPython.display import Markdown Markdown(r.text) ---- ## About those questions In general, question 1 should be pretty approachable for all learners. It shouldn't take more than a few lines of code to solve — and you might be able to do it in one! Question 2 should be solvable by most beginners, but it will take a bit more work. It probably needs at least a few lines of code. It will probably involve more than one data structure. You might need loops or NumPy, depending on the type of problem. Question 3 is supposed to be a bit tricky. It should be solvable by beginners, but it might take you an hour or two. If there is a question 4, it will be about the same difficulty as Question 3. ---- ## Getting your input When you think you've figured out how to solve the problem, or at least have a go at Question 1, you'll need some unique input that's only for you, so we need to pass some information to the server. Choose a value for `'key'` — it can be any string, like your name, or your favourite word. It is not stored on the server or interpreted in any way, it's just a string that acts as a unique identifier. It ensures that you will probably get your own input, unlike anyone else's. my_key = "honey badger" params = {'key': my_key} r = requests.get(url, params) r.text This input will be the same for all of the questions. You can now attempt to answer the questions. ---- ## Giving your answer Running your solution on your unique input gives you an answer. You can give this answer to the server and it will tell you if you are correct or not: params = {'key': my_key, # <--- must be the same key as before 'question': 1, # <--- which question you're answering 'answer': 1234, # <--- your answer to that question } r = requests.get(url, params) r.text ---- ## Tips - Try to write functions to solve the problems. You often need to build on what you did in previous questions, and re-using code like this is much easier if you use functions. - Before answering most of the questions, you'll probably need to convert the data you are given, which is one long string, into something more digestible. For example, you might want to convert a bunch of filenames into a list of strings. Sometimes, a dictionary representation might help. - The questions are solvable with pure Python code, but it might sometimes help to use other libraries. If the other libraries are _really_ helpful, we'll let you know about them in the question. If you want to use Pandas or NumPy, that's definitely fine — and some questions will be easier if you do. - Do chat with others on [Software Underground](https://www.softwareunderground.org/) if you get stuck. - We'll be sending example solutions as we unlock new puzzles each week. ---- © 2024 [Scienxlab](https://scienxlab.org/) — Code: openly licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) — Text: openly licensed under [CC BY](https://creativecommons.org/licenses/by/4.0/).