-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📦 NEW: Project Euler 22 | Added solution and test cases
- Loading branch information
1 parent
8d15caf
commit d067a20
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
/* | ||
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, reject) => { | ||
fs.readFile(filePath, 'utf8', (err, data) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
|
||
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 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) | ||
}) | ||
}) |