Created
October 6, 2024 15:47
-
-
Save Kenya-West/dbfe3cb502fa8ff814caf4fe5d50b138 to your computer and use it in GitHub Desktop.
A ChatGPT snippet to output and sort the best (in terms of beauty) phone number to pick
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
// Helper function to check if all characters in a string are the same | |
function allSame(str) { | |
return str.split('').every(char => char === str[0]); | |
} | |
// Helper function to check if a string is a palindrome | |
function isPalindrome(str) { | |
return str === str.split('').reverse().join(''); | |
} | |
// Helper function to check if a string has sequential numbers (either increasing or decreasing) | |
function isSequential(str) { | |
let isIncreasing = true; | |
let isDecreasing = true; | |
for (let i = 0; i < str.length - 1; i++) { | |
if (parseInt(str[i]) + 1 !== parseInt(str[i + 1])) { | |
isIncreasing = false; | |
} | |
if (parseInt(str[i]) - 1 !== parseInt(str[i + 1])) { | |
isDecreasing = false; | |
} | |
} | |
return isIncreasing || isDecreasing; | |
} | |
// Helper function to calculate the sum of differences between nearby digits | |
function nearbyDigitDiversity(str) { | |
let totalDifference = 0; | |
for (let i = 0; i < str.length - 1; i++) { | |
totalDifference += Math.abs(parseInt(str[i]) - parseInt(str[i + 1])); | |
} | |
return totalDifference; | |
} | |
// Helper function to calculate digits entropy (more unique digits -> higher entropy) | |
function digitEntropy(str) { | |
const digitFrequency = {}; | |
for (let i = 0; i < str.length; i++) { | |
const digit = str[i]; | |
digitFrequency[digit] = (digitFrequency[digit] || 0) + 1; | |
} | |
// Entropy is higher when more digits are unique | |
return Object.keys(digitFrequency).length; | |
} | |
// Function to find beautiful phone numbers and sort by nearby digits and entropy | |
function findAndSortBeautifulPhoneNumbers(phoneNumbers) { | |
const beautifulNumbers = []; | |
phoneNumbers.forEach(number => { | |
// Remove spaces and other symbols to focus only on digits | |
const cleanedNumber = number.replace(/[^\d]/g, ''); | |
// Extract parts: XXX XXX XX XX | |
const part1 = cleanedNumber.slice(1, 4); // XXX | |
const part2 = cleanedNumber.slice(4, 7); // XXX | |
const part3 = cleanedNumber.slice(7, 9); // XX | |
const part4 = cleanedNumber.slice(9, 11); // XX | |
const fullNumber = part1 + part2 + part3 + part4; | |
// Check if it's beautiful by existing conditions | |
if ( | |
allSame(part1) || allSame(part2) || allSame(part3) || allSame(part4) || // Same digits in any part | |
isPalindrome(fullNumber) || // Full number palindrome | |
isSequential(fullNumber) // Sequential digits | |
) { | |
// Add to beautifulNumbers with its score | |
beautifulNumbers.push({ | |
number, | |
nearbyDiversityScore: nearbyDigitDiversity(fullNumber), | |
entropyScore: digitEntropy(fullNumber) | |
}); | |
} | |
}); | |
// Sort by nearby digit diversity, then by entropy (lower is better) | |
beautifulNumbers.sort((a, b) => { | |
if (a.nearbyDiversityScore === b.nearbyDiversityScore) { | |
return a.entropyScore - b.entropyScore; // Sort by entropy if diversity is the same | |
} | |
return a.nearbyDiversityScore - b.nearbyDiversityScore; // Otherwise, sort by nearby diversity | |
}); | |
return beautifulNumbers.map(obj => obj.number); // Return only sorted phone numbers | |
} | |
// Example array of phone numbers | |
const phoneNumbers = [ | |
"+7 123 456 78 90", | |
"+7 999 999 99 99", | |
"+7 111 222 33 44", | |
"+7 987 654 32 10", | |
"+7 123 321 12 31", | |
"+7 444 444 44 44", | |
"+7 543 210 98 76" | |
]; | |
// Sort beautiful numbers | |
const sortedBeautifulNumbers = findAndSortBeautifulPhoneNumbers(phoneNumbers); | |
console.log("Sorted Beautiful Numbers:", sortedBeautifulNumbers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment