HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Raise a list of integers to powers and return uniques

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
returnuniquesraiseandlistintegerspowers

Problem

Problem (inspired by Project Euler 29):


Consider all integer combinations of ab for X ≤ a ≤ Y
and X ≤ b ≤ Y


Return a sequence of distinct terms in numerical order

-
Input is a list of 2 integers separated by a space (" "), representing the lower and upper bounds (X Y). Input is read from txt file.

-
Output is a numerically sorted list of all unique numbers generated by the function (ab) , separated by space. Output is written to txt file.

Example:

Input:  2 5
Output: 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125


And the code:

```
Option Explicit On
Option Strict On
Option Infer On
Option Compare Text
Imports System
Imports System.IO
Imports System.Windows.Forms
Module ProjectEuler29

Sub Main()
Dim myBounds = GetInput("C:\temp\square.txt")
Dim myResults = UseBoundsToCreateList(myBounds)
WriteToFile("C:\temp\square-answer.txt", myResults)
End Sub

Private Function GetInput(ByVal inputPath As String) As Integer()
Dim returnText As String = File.ReadAllText(inputPath)
Dim returnArray As String() = returnText.Split(New String() {}, StringSplitOptions.None)
Return ConvertStringToIntegerArray(returnArray)
End Function

Private Function ConvertStringToIntegerArray(ByVal arrayOfStrings As String()) As Integer()
Return Array.ConvertAll(arrayOfStrings, Function(str) Int32.Parse(str))
End Function

Private Function UseBoundsToCreateList(ByVal myBounds As Integer()) As String
Dim numberOfIntegers = myBounds(myBounds.Length - 1) - myBounds(0) + 1
Dim resultList As New List(Of Double)
Dim i As Integer
Dim myResult As Double
For i = myBounds(0) To myBounds(0) + numberOfIntegers - 1
For j = myBounds(0) To myBounds(0) + numberOfIntegers - 1
myResult = i ^ j
If Not resultList.Contains(myResult) Then resultList.Add(myResult)
Next
Next
resultList.Sort()

Solution

Use a Hashset

This seems to be a more intuitive data structure to use since you need all of your values to be unique.

The code would change very little, but you could eliminate checking if the value is already in the List.

Use built in functions before creating your own methods

When writing to a file, you shouldn't need to check if a file doesn't exist and explicitly create it. This should happen automatically.

Use the System.IO.WriteAllText method

This also closes the file automatically, freeing you from needing to do that manually. (It basically replaces the method you wrote, so you may be able to get rid of that entirely).

You've already done similar in your GetInput function by using File.ReadAllText

Only use a one line method if it increases readability

Private Function ConvertStringToIntegerArray(ByVal arrayOfStrings As String()) As Integer()
    Return Array.ConvertAll(arrayOfStrings, Function(str) Int32.Parse(str))
End Function


The above method doesn't really have a purpose. It doesn't increase readability much since the build in method explains pretty well what it does with the name ConvertAll. Of course if you feel that your method name makes your code more human readable you should use it.

Code Snippets

Private Function ConvertStringToIntegerArray(ByVal arrayOfStrings As String()) As Integer()
    Return Array.ConvertAll(arrayOfStrings, Function(str) Int32.Parse(str))
End Function

Context

StackExchange Code Review Q#141583, answer score: 3

Revisions (0)

No revisions yet.