-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Project euler 22 #1726
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
Open
Open
Project euler 22 #1726
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b7309f0
📦 NEW: Added solution for ProjectEuler-007
omkarnathparida d3a3b33
🐛 FIX: Spelling mistake fixes
omkarnathparida e9b5a6a
👌 IMPROVE: changed variable name from `inc` to `candidateValue` and t...
omkarnathparida e99c722
👌 IMPROVE: Modified the code
omkarnathparida 5ac898f
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 0f9f1ba
👌 IMPROVE: Added test case for ProjectEuler Problem001
omkarnathparida 3caad5c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 b7584fd
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 6693f9c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 0830570
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 4d7149c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 53e3938
👌 IMPROVE: Added test cases for Project Euler Problem 4
omkarnathparida 96224e7
👌 IMPROVE: auto prettier fixes
omkarnathparida 718d515
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 396782d
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 944077b
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 8d15caf
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 31aca33
📦 NEW: Project Euler 22 | Added solution and test cases
omkarnathparida d067a20
📦 NEW: Project Euler 22 | Added solution and test cases
omkarnathparida fb558fb
Updated Documentation in README.md
pomkarnath98 15600f4
Merge branch 'master' into project-euler-22
omkarnathparida 8d2c4b4
👌 IMPROVE: code improvement for codecov full coverage
omkarnathparida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY", | ||
"LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE", | ||
"LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA", | ||
"AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE", | ||
"CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE", | ||
"JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE", | ||
"JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY", | ||
"THERESA","BEVERLY","DENISE","TAMMY","IRENE","JANE","LORI","RACHEL","MARILYN","ANDREA", | ||
"KATHRYN","LOUISE","SARA","ANNE","JACQUELINE","WANDA","BONNIE","JULIA","RUBY","LOIS", | ||
"TINA","PHYLLIS","NORMA","PAULA","DIANA","ANNIE","LILLIAN","EMILY","ROBIN","PEGGY", | ||
"CRYSTAL","GLADYS","RITA","DAWN","CONNIE","FLORENCE","TRACY","EDNA","TIFFANY","CARMEN", | ||
"ROSA","CINDY","GRACE","WENDY","VICTORIA","EDITH","KIM","SHERRY","SYLVIA","JOSEPHINE", | ||
"THELMA","SHANNON","SHEILA","ETHEL","ELLEN","ELAINE","MARJORIE","CARRIE","CHARLOTTE","MONICA", | ||
"ESTHER","PAULINE","EMMA","JUANITA","ANITA","RHONDA","HAZEL","AMBER","EVA","DEBBIE", | ||
"APRIL","LESLIE","CLARA","LUCILLE","JAMIE","JOANNE","ELEANOR","VALERIE","DANIELLE","MEGAN", | ||
"ALICIA","SUZANNE","MICHELE","GAIL","BERTHA","DARLENE","VERONICA","JILL","ERIN","GERALDINE", | ||
"LAUREN","CATHY","JOANN","LORRAINE","LYNN","SALLY","REGINA","ERICA","BEATRICE","DOLORES", | ||
"BERNICE","AUDREY","YVONNE","ANNETTE","JUNE","SAMANTHA","MARION","DANA","STACY","ANA", | ||
"RENEE","IDA","VIVIAN","ROBERTA","HOLLY","BRITTANY","MELANIE","LORETTA","YOLANDA","JEANETTE", | ||
"LAURIE","KATIE","KRISTEN","VANESSA","ALMA","SUE","ELSIE","BETH","JEANNE","VICKI", | ||
"CARLA","TARA","ROSEMARY","EILEEN","TERRI","GERTRUDE","LUCY","TONYA","ELLA","STACEY", | ||
"WILMA","GINA","KRISTIN","JESSIE","NATALIE","AGNES","VERA","WILLIE","CHARLENE","BESSIE", | ||
"DELORES","MELINDA","PEARL","ARLENE","MAUREEN","COLLEEN","ALLISON","TAMARA","JOY","GEORGIA", | ||
"CONSTANCE","LILLIE","CLAUDIA","JACKIE","MARCIA","TANYA","NELLIE","MINNIE","MARLENE","HEIDI", | ||
"GLENDA","LYDIA","VIOLA","COURTNEY","MARIAN","STELLA","CAROLINE","DORA","JO","VICKIE", | ||
"MATTIE","TERRY","MAXINE","IRMA","MABEL","MARSHA","MYRTLE","LENA","CHRISTY","DEANNA", | ||
"PATSY","HILDA","GWENDOLYN","JENNIE","NORA","MARGIE","NINA","CASSANDRA","LEAH","PENNY", | ||
"KAY","PRISCILLA","NAOMI","CAROLE","BRANDY","OLGA","BILLIE","DIANNE","TRACEY","LEONA", | ||
"JENNY","FELICIA","SONIA","MIRIAM","VELMA","BECKY","BOBBIE","VIOLET","KRISTINA","TONI", | ||
"MISTY","MAE","SHELLY","DAISY","RAMONA","SHERRI","ERIKA","KATRINA","CLAIRE","LINDSEY", | ||
"LINDSAY","GENEVA","GUADALUPE","BELINDA","MARGARITA","SHERYL","CORA","FAYE","ADA","NATASHA", | ||
"SABRINA","ISABEL","MARGUERITE","HATTIE","HARRIET","MOLLY","CECILIA","KRISTI","BRANDI","BLANCHE", | ||
"SANDY","ROSIE","JOANNA","IRIS","EUNICE","ANGIE","INEZ","LYNDA","MADELINE","AMELIA", | ||
"ALYSSA","KRISTY","CATHLEEN","ANGELINE","FRIEDA","ANTOINETTE","CECIL","JASMINE","NORMA","HANNAH", | ||
"SANDRA","KEISHA","CLAIRE","ELLA","TAMIKA","FANNIE","CELIA","PEGGY","MICHAELA","ALYSON", | ||
"ALTHEA","KENDRA","LORA","CORA","SHIRLEY","MATTIE","EMMA","MONICA","CATHY","ELEANOR", | ||
"REBECCA","TERESA","CLARA","PAMELA","SYLVIA","NANCY","FLORENCE","VICTORIA","ANITA","ELIZABETH", | ||
"JESSIE","LORRAINE","GERTRUDE","KATHERINE","VIVIAN","LOUISE","SUSAN","WANDA","PATRICIA","MARY","OMKARNATH" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Names Scores | ||
|
||
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. | ||
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 +たす 15 +たす 12 +たす 9 +たす 14 =わ 53, is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714. | ||
What is the total of all the name scores in the file? | ||
*/ | ||
import fs from 'fs' | ||
|
||
export const getAlphabeticalValue = (name) => { | ||
return [...name].reduce( | ||
(sum, char) => sum + (char.charCodeAt(0) - 'A'.charCodeAt(0) + 1), | ||
0 | ||
) | ||
} | ||
|
||
export const calculateTotalNameScore = (filePath) => { | ||
return new Promise((resolve) => { | ||
fs.readFile(filePath, 'utf8', (_, data) => { | ||
const names = data.replace(/"/g, '').split(',').sort() | ||
|
||
const totalScore = names.reduce((total, name, index) => { | ||
const alphabeticalValue = getAlphabeticalValue(name) | ||
const nameScore = alphabeticalValue * (index + 1) | ||
return total + nameScore | ||
}, 0) | ||
|
||
resolve(totalScore) | ||
}) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { calculateTotalNameScore, getAlphabeticalValue } from './Problem022' | ||
const path = require('path') | ||
|
||
describe('getAlphabeticalValue', () => { | ||
it('should return correct alphabetical value for COLIN', () => { | ||
const name = 'COLIN' | ||
const expectedValue = 3 + 15 + 12 + 9 + 14 | ||
expect(getAlphabeticalValue(name)).toBe(expectedValue) | ||
}) | ||
|
||
it('should return correct alphabetical value for A', () => { | ||
const name = 'A' | ||
const expectedValue = 1 | ||
expect(getAlphabeticalValue(name)).toBe(expectedValue) | ||
}) | ||
|
||
it('should return correct alphabetical value for Z', () => { | ||
const name = 'Z' | ||
const expectedValue = 26 | ||
expect(getAlphabeticalValue(name)).toBe(expectedValue) | ||
}) | ||
}) | ||
|
||
describe('calculateTotalNameScore', () => { | ||
it('should correctly calculate the total name score', async () => { | ||
const namesFilePath = path.join(__dirname, 'Names.txt') | ||
const result = await calculateTotalNameScore(namesFilePath) | ||
|
||
const expectedScore = 4654143 | ||
expect(result).toBe(expectedScore) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.