mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update compress challenge
Fix some bugs
This commit is contained in:
parent
d29f1ae528
commit
5a79116c0c
|
@ -2,30 +2,21 @@
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
|
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Solution Notebook"
|
"# Solution Notebook"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n",
|
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -33,15 +24,14 @@
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code](#Code)\n",
|
||||||
"* [Unit Test](#Unit-Test)"
|
"* [Unit Test](#Unit-Test)\n",
|
||||||
|
"* [Bonus C Algorithm](#C-Algorithm)\n",
|
||||||
|
"* [Bonus C Code](#C-Code)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -58,10 +48,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -73,10 +60,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -102,10 +86,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Code"
|
"## Code"
|
||||||
]
|
]
|
||||||
|
@ -114,9 +95,7 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false,
|
"collapsed": false
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -144,10 +123,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Unit Test"
|
"## Unit Test"
|
||||||
]
|
]
|
||||||
|
@ -156,9 +132,7 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false,
|
"collapsed": false
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
@ -216,9 +190,104 @@
|
||||||
"source": [
|
"source": [
|
||||||
"%run -i test_compress.py"
|
"%run -i test_compress.py"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## C Algorithm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This algorithm is based on Python Algorithm\n",
|
||||||
|
"\n",
|
||||||
|
"Define a 'result' (the compressed string) with the string's len .\n",
|
||||||
|
"\n",
|
||||||
|
"* First, for each element into the string\n",
|
||||||
|
" * If the element (char) is as the last char, we increment the count\n",
|
||||||
|
" * Else\n",
|
||||||
|
" * Concat the last char and count into the 'result'.\n",
|
||||||
|
" * make the last char = char\n",
|
||||||
|
" * count = 1\n",
|
||||||
|
" \n",
|
||||||
|
"* Then, concat the last char and count into the 'result' \n",
|
||||||
|
"\n",
|
||||||
|
"* Finally, if the 'result' (compressed string) size is < string size\n",
|
||||||
|
" * Return 'result' string\n",
|
||||||
|
"* Else\n",
|
||||||
|
" * Return string"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# %load compress.c\n",
|
||||||
|
"#include <stdio.h>\n",
|
||||||
|
"#include <stdlib.h>\n",
|
||||||
|
"#include <string.h>\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"char *compress (char *s);\n",
|
||||||
|
"char *_calc_partial_result (char p_char, char count);\n",
|
||||||
|
"\n",
|
||||||
|
"int main ( ){\n",
|
||||||
|
" printf(\"%s\\n\",compress(\"AABBCC\"));\n",
|
||||||
|
" return 0;\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"char *compress ( char *s){\n",
|
||||||
|
" char *result = (char*) malloc(strlen(s)); // result string\n",
|
||||||
|
" char prev_char; // previous char\n",
|
||||||
|
" int count = 0; // count defines\n",
|
||||||
|
" char *p_count;\n",
|
||||||
|
" int i;\n",
|
||||||
|
" if(sizeof(s) == 0)\n",
|
||||||
|
" return NULL;\n",
|
||||||
|
" prev_char = s[0];\n",
|
||||||
|
" for (i=0; i<strlen(s); i+=1){\n",
|
||||||
|
" if (s[i] == prev_char)\n",
|
||||||
|
" count+=1;\n",
|
||||||
|
" else{\n",
|
||||||
|
" asprintf(&p_count, \"%i\", count);\n",
|
||||||
|
" strcat(result, _calc_partial_result(prev_char, *p_count));\n",
|
||||||
|
" prev_char = s[i];\n",
|
||||||
|
" count = 1;\n",
|
||||||
|
" }\n",
|
||||||
|
" }\n",
|
||||||
|
" asprintf(&p_count, \"%i\", count);\n",
|
||||||
|
" strcat(result, _calc_partial_result(prev_char, *p_count));\n",
|
||||||
|
" if(strlen(result)<strlen(s))\n",
|
||||||
|
" return result;\n",
|
||||||
|
" else\n",
|
||||||
|
" return s;\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"char *_calc_partial_result (char p_char, char count){\n",
|
||||||
|
" char *buff;\n",
|
||||||
|
" int c = atoi(&count);\n",
|
||||||
|
" if(c > 1){\n",
|
||||||
|
" buff = (char*) malloc(sizeof(p_char) + sizeof(count));\n",
|
||||||
|
" strcpy(buff, &p_char);\n",
|
||||||
|
" strcat(buff, &count);\n",
|
||||||
|
" }else{\n",
|
||||||
|
" buff = (char*) malloc(sizeof(p_char));\n",
|
||||||
|
" strncpy(buff, &p_char, sizeof(p_char));\n",
|
||||||
|
" }\n",
|
||||||
|
" return buff;\n",
|
||||||
|
"}\n"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
"celltoolbar": "Edit Metadata",
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 2",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
|
@ -227,14 +296,14 @@
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 3
|
"version": 2
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython2",
|
||||||
"version": "3.5.0"
|
"version": "2.7.9"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user