Code Monkey home page Code Monkey logo

dynamic_programming_c's Introduction

Hi there, I'm Dennis ๐Ÿ‘‹

Languages and Tools:

arduino c cplusplus git java matlab JavaScript SQLite

SchattenMonarch's GitHub stats

Click for available ranks "Note: Available ranks are S (top 1%), A+ (12.5%), A (25%), A- (37.5%), B+ (50%), B (62.5%), B- (75%), C+ (87.5%) and C (everyone). The values are calculated by using the cumulative distribution function using commits, contributions, issues, stars, pull requests, followers, and owned repositories. The implementation can be investigated at src/calculateRank.js." (source)

GitHub Streak

dynamic_programming_c's People

Contributors

schattenmonarch avatar

Stargazers

 avatar  avatar

Watchers

 avatar

dynamic_programming_c's Issues

fix a bug

getting a value from the hash map fails.

optimize the program in can_sum.c and improve the time complexity by using memoization

This is an improvement of can_sum brute force

Return values:

  • true: if it is possible to generate the target_value by summing the elements in the array.
  • false: if it is not possible.

Samples

Sample 1

can_sum(7, (int[]){ 5, 3, 4, 7 }, 4) -> true

because there are three possible solutions: 3+4, 4+3 and 7.

Sample 2

can_sum(7, { 2, 4 }, 2) -> false

because it's impossible to build a 7 with the numbers in the array.

Sample 3

can_sum(300, (int[]){ 7,  4 }, 2) -> false

this function call would take a really long time with a bad time complexity.

Fix Bug in grid_traveler.c

Bug

Program finds keys that were not inserted.

Console output

image

Source Code grid_traveler.c

Folder with all necessary files

#define _CRT_SECURE_NO_WARNINGS
#include "grid_traveler.h"
#include "khash.h"
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
//#define KEY_IN_HASH_MAP (it != kh_end(map_ptr) && kh_str_hash_func(key) == kh_str_hash_func(kh_key(map_ptr, it)))
#define KEY_BUFFER_SIZE 15u

KHASH_MAP_INIT_STR(map, long long)
khash_t(map)* map_ptr; 
khiter_t it;
int ret;
long long val;

void init_grid_traveler(void) {
	map_ptr = kh_init(map);
	it = -1;
	ret = -1;
	val = -1;
}

long long grid_traveler(int row, int column) {
	char key[KEY_BUFFER_SIZE], found_key[KEY_BUFFER_SIZE];
	sprintf(key, "<%d , %d>", row, column);
	printf("ENTERED recursive function with key: %s\n", key);
	if (row == 1 && column == 1) {
		puts("return 1;");
		return 1;
	} 
	if (row == 0 || column == 0) {
		puts("return 0;");
		return 0;
	}
	
	
	it = kh_get(map, map_ptr, key);
	int key_hash, found_key_hash;
	if (it != kh_end(map_ptr)) {
		sprintf(found_key, "%s", kh_key(map_ptr, it));
		key_hash = kh_str_hash_func(key);
		found_key_hash = kh_str_hash_func(found_key);
	}
	
	bool KEY_IN_HASH_MAP = (it != kh_end(map_ptr) && kh_str_hash_func(key) == kh_str_hash_func(kh_key(map_ptr, it)));
	if (KEY_IN_HASH_MAP) {
		printf("Key %s / found key %s found in hash_map with the value %lld\n", key, kh_key(map_ptr, it), kh_val(map_ptr, it));
		return kh_val(map_ptr, it);
	}
	else {
		val = grid_traveler(row - 1, column) + grid_traveler(row, column - 1);
		it = kh_put(map, map_ptr, key, &ret);
		assert(ret != -1);
		kh_val(map_ptr, it) = val;
		printf("Key %s value was CALCULATED: %lld and inserted into the hash_map with the key: %s\n", key, kh_val(map_ptr, it), kh_key(map_ptr, it));
	}
	return kh_val(map_ptr, it);
}

void deinit_grid_traveler(void) {
	free(map_ptr);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.