mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update compress challenge
Add C Algorithm to resolve the compress string challenge
This commit is contained in:
parent
fcd4e52e91
commit
d29f1ae528
52
arrays_strings/compress/compress.c
Normal file
52
arrays_strings/compress/compress.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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<strlen(s); i+=1){
|
||||
if (s[i] == prev_char)
|
||||
count+=1;
|
||||
else{
|
||||
asprintf(&p_count, "%i", count);
|
||||
strcat(result, _calc_partial_result(prev_char, *p_count));
|
||||
prev_char = s[i];
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
asprintf(&p_count, "%i", count);
|
||||
strcat(result, _calc_partial_result(prev_char, *p_count));
|
||||
if(strlen(result)<strlen(s))
|
||||
return result;
|
||||
else
|
||||
return s;
|
||||
}
|
||||
|
||||
char *_calc_partial_result (char p_char, char count){
|
||||
char *buff;
|
||||
int c = atoi(&count);
|
||||
if(c > 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;
|
||||
}
|
|
@ -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": {
|
||||
|
|
Loading…
Reference in New Issue
Block a user