Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Node Modules
.node_module
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@

## Algorithms
# Fantz

My lab works of Algorithms classes as a student of "Internet of Things" program in Lviv Polytechnic National University.
## Task

Beavers Antin and Ori came up with a game - to invent random binary numbers, and to see if these numbers can be broken into pieces, each of which is the power of the number X in the decimal system.

For example, if `X == 5`, then the binary number `101110011` can be divided into `101`, `11001` and `1`, each of which is 5 to some degree (`101` in decimal `== 5 == 5 ^ 1`, `11001 == 25 == 5 ^ 2` and `1 == 1 is 5 ^ 0`).

Demonstrate that humans are smarter than beavers, and for a given binary number `N`, find the smallest number of pieces to break it into.

---

### Input data:
The first line contains X - a sequence of zeros and ones and N.

### Output data:
The smallest number of pieces into which we can divide the input number, or -1 if this is not possible.

### Limit:
+ `0 < len (X) < 100`
+ `0 < N < 100`

---

## Examples

In: `101101101` `5`

Out: `3`


In: `1111101` `5`

Out: `1`
51 changes: 51 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const str = '101101101';
const number = 5;

export let binaryNumbersArray = [];

// convert all number^x to binary
export function createBinaryArray(number) {
let i = 0; //
let binaryNumber = ""; // number converted to binary

while (binaryNumber.length <= str.length) {
let powItem = Math.pow(number, i); // exponentiation number
binaryNumber = powItem.toString(2);
binaryNumbersArray.push(binaryNumber)
i++;
}
}

export function findMinDivides(str) {
let startIndex = 1;
let strArray = str.split('');

let diviceCount = 0;

while (startIndex <= strArray.length) {
// if there is a match
if(binaryNumbersArray.includes(strArray.slice(0, startIndex).join(''))) {
// if the whole string matches powered number, the minimum number of dividing is 1
if(startIndex == strArray.length)
return 1

let rightDivideCount = findMinDivides(str.slice(startIndex));

// otherwise check if right part can be divided and assign
if(rightDivideCount !== 0) {
if(diviceCount == 0 || diviceCount > rightDivideCount + 1)
diviceCount = rightDivideCount + 1;
}
}
startIndex++;
}

return diviceCount;
}

createBinaryArray(number)
console.log(binaryNumbersArray);
console.log(findMinDivides(str));

// const last = binaryNumbersArray.pop();
// console.log(binaryNumbersArray);
Loading