gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)
Hi
- User
Hi
- Assistant
Hello! How can I assist you today?
- User
plese, write function on TypeScript that find all ranges inside array. For example, if array=[0,1,2,5] result must be "[0-2,5]"
- Assistant
Sure, I can help you with that. Here's a TypeScript function that finds all the sequential ranges inside an array:
function findRanges(arr: number[]): string { let ranges: string[] = []; let start: number = arr[0]; let end: number = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] === end + 1) { // The current element is part of the current range end = arr[i]; } else { // The current element is not part of the current range if (start !== end) { ranges.push(`${start}-${end}`); } else { ranges.push(`${start}`); } start = arr[i]; end = arr[i]; } } // Push the last range onto the result if applicable if (start !== end) { ranges.push(`${start}-${end}`); } else { ranges.push(`${start}`); } return ranges.join(); } // Example usage: const array = [0, 1, 2, 5]; const result = findRanges(array); console.log(result); // Output: "0-2,5"I hope this helps! Let me know if you have any further questions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →