Update compress challenge

Fix some bugs
This commit is contained in:
eamanu 2017-04-12 16:52:55 -03:00
parent d29f1ae528
commit 5a79116c0c

View File

@ -2,30 +2,21 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"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)."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n",
"\n",
@ -33,15 +24,14 @@
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\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",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Constraints\n",
"\n",
@ -58,10 +48,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
@ -73,10 +60,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
@ -102,10 +86,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Code"
]
@ -114,9 +95,7 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
"collapsed": false
},
"outputs": [],
"source": [
@ -144,10 +123,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Unit Test"
]
@ -156,9 +132,7 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
"collapsed": false
},
"outputs": [
{
@ -216,9 +190,104 @@
"source": [
"%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": {
"celltoolbar": "Edit Metadata",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
@ -227,14 +296,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,