From d29f1ae528fb36c31eeed7b1c52775d80c0f1eea Mon Sep 17 00:00:00 2001 From: eamanu Date: Wed, 12 Apr 2017 16:37:01 -0300 Subject: [PATCH] Update compress challenge Add C Algorithm to resolve the compress string challenge --- arrays_strings/compress/compress.c | 52 +++++++++++++++++ .../compress/compress_solution.ipynb | 56 ++++++++++++++----- 2 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 arrays_strings/compress/compress.c diff --git a/arrays_strings/compress/compress.c b/arrays_strings/compress/compress.c new file mode 100644 index 0000000..ca6858b --- /dev/null +++ b/arrays_strings/compress/compress.c @@ -0,0 +1,52 @@ +#include +#include +#include + +char *compress (char *s); +char *_calc_partial_result (char p_char, char count); +int main ( ){ + printf("%s\n",compress("AABBCC")); + return 0; +} + + +char *compress ( char *s){ + char *result = (char*) malloc(strlen(s)); // result string + char prev_char; // previous char + int count = 0; // count defines + char *p_count; + int i; + if(sizeof(s) == 0) + return NULL; + prev_char = s[0]; + for (i=0; i 1){ + buff = (char*) malloc(sizeof(p_char) + sizeof(count)); + strcpy(buff, &p_char); + strcat(buff, &count); + }else{ + buff = (char*) malloc(sizeof(p_char)); + strncpy(buff, &p_char, sizeof(p_char)); + } + return buff; +} diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index fbceb59..1786429 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -2,21 +2,30 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "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": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "# Solution Notebook" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n", "\n", @@ -29,7 +38,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Constraints\n", "\n", @@ -46,7 +58,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Test Cases\n", "\n", @@ -58,7 +73,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Algorithm\n", "\n", @@ -84,7 +102,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Code" ] @@ -93,7 +114,9 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -121,7 +144,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Unit Test" ] @@ -130,7 +156,9 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [ { @@ -172,7 +200,9 @@ "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [ { @@ -190,9 +220,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": {