diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e73e6a9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,11 @@ +name: Test Code +on: + push: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: npm i jsverify + - run: node code.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4f0014 --- /dev/null +++ b/.gitignore @@ -0,0 +1,166 @@ +# Created by https://www.toptal.com/developers/gitignore/api/node,vim +# Edit at https://www.toptal.com/developers/gitignore?templates=node,vim + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-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/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# 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 variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# 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 + +# vuepress v2.x temp and cache directory +.temp + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +### Node Patch ### +# Serverless Webpack directories +.webpack/ + +# Optional stylelint cache + +# SvelteKit build / generate output +.svelte-kit + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# End of https://www.toptal.com/developers/gitignore/api/node,vim + diff --git a/README.md b/README.md index 9bad5b0..8303fe2 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,10 @@ this. What is the time complexity of your implementation (worst-case $\Theta$)? Add your answer, including your reasoning, to this markdown file. + +### Analysis + +Since comparisons take constant time and we consider n elements of the array the complexity is $\Theta(n)$ + +I certify that I have listed all sources used to complete this exercise, including the use of any Large Language Models. All of the work is my own, except where stated otherwise. I am aware that plagiarism carries severe penalties and that if plagiarism is suspected, charges may be filed against me without prior notice. + diff --git a/code.js b/code.js new file mode 100644 index 0000000..f3a087f --- /dev/null +++ b/code.js @@ -0,0 +1,17 @@ +var async = require("async"); + +//sources: queried chat gpt what function in the async library would +//be useful for counting matches in an array and it suggested "each". Then I read the docs for the rest. +function count(a, k) { + var m = 0; + + async.each( + a, + (v, callback) => { + if (v == k) { m++; } + callback(); + } + ); + + return m; +} diff --git a/code.test.js b/code.test.js new file mode 100644 index 0000000..1a0b57c --- /dev/null +++ b/code.test.js @@ -0,0 +1,19 @@ +const fs = require('fs'); +const jsc = require('jsverify'); + +eval(fs.readFileSync('code.js')+''); + +const testSum = + jsc.forall("array nat","nat", function(arr, key) { + return JSON.stringify(count(arr, key)) == JSON.stringify(syncCount(arr, key)); + }); + +jsc.assert(testSum); + +function syncCount(a, k) { + var m = 0; + for (var i = 0; i < a.length; i++) { + if (a[i] == k) { m++; } + } + return m; +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..8efa40c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,74 @@ +{ + "name": "asynchronous-functions-DojeeDev", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "async": "^3.2.6", + "fs": "^0.0.1-security", + "jsverify": "^0.8.4" + } + }, + "node_modules/async": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", + "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==", + "license": "MIT" + }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==", + "license": "ISC" + }, + "node_modules/jsverify": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/jsverify/-/jsverify-0.8.4.tgz", + "integrity": "sha512-nUG73Sfi8L4eOkc7pv9sflgAm43v+z6XMuePGVdRoBUxBLJiVcMcf3Xgc4h19eHHF3JwsaagOkUu825UnPBLJw==", + "license": "MIT", + "dependencies": { + "lazy-seq": "^1.0.0", + "rc4": "~0.1.5", + "trampa": "^1.0.0", + "typify-parser": "^1.1.0" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/lazy-seq": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lazy-seq/-/lazy-seq-1.0.0.tgz", + "integrity": "sha512-AQ4vRcnULa7FX6R6YTAjKQAE1MuEThidVQm0TEtTpedaBpnOwid5k6go16E5NDkafel1xAsZL73WkwdG03IzhA==", + "license": "MIT", + "engines": { + "node": ">= 0.10.0" + } + }, + "node_modules/rc4": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/rc4/-/rc4-0.1.5.tgz", + "integrity": "sha512-xdDTNV90z5x5u25Oc871Xnvu7yAr4tV7Eluh0VSvrhUkry39q1k+zkz7xroqHbRq+8PiazySHJPArqifUvz9VA==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/trampa": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/trampa/-/trampa-1.0.1.tgz", + "integrity": "sha512-93WeyHNuRggPEsfCe+yHxCgM2s6H3Q8Wmlt6b6ObJL8qc7eZlRaFjQxwTrB+zbvGtlDRnAkMoYYo3+2uH/fEwA==", + "license": "MIT" + }, + "node_modules/typify-parser": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/typify-parser/-/typify-parser-1.1.0.tgz", + "integrity": "sha512-p5+L1sc6Al3bcStMwiZNxDh4ii4JxL+famEbSIUuOUMVoNn9Nz27AT1jL3x7poMHxqKK0UQIUAp5lGkKbyKkFA==", + "license": "MIT", + "engines": { + "node": ">= 0.10.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..22862bd --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "async": "^3.2.6", + "fs": "^0.0.1-security", + "jsverify": "^0.8.4" + } +}