algorithms involving unknowns

  • 6 Replies
  • 880 Views
*

yotamarker

  • Trusty Member
  • ********
  • Replicant
  • *
  • 602
    • battle programming
algorithms involving unknowns
« on: December 23, 2017, 08:35:45 pm »
the following is supposed to pick the closest elevator to person x going up or down from floor y

do you agree with the alg or do you have a better solution :
Code: [Select]
Public Class Form1
    Dim topFloor As Integer = 31
    Dim btmFlr As Integer = 0
    Dim EF() As Integer = {1, 6, 12, 18, 24, 31, 20} ' elevator at floor
    Dim ar() As Integer = {1, 2, 0, 1, 2, 0} ' elevator direction
    '1 up 2 down 0 non moving
    Private Sub inputReqBtn_Click(sender As Object, e As EventArgs) Handles inputReqBtn.Click
        Dim Rqflr As Integer = RqflrTxt.Text
        Dim Rqdir As Integer = RqDirTxt.Text

        MsgBox(nearElevator(Rqflr, Rqdir, EF, ar))
    End Sub
    Function elevatorDistance(ByVal xF As Integer, ByVal ef As Integer, ByVal xdir As Integer, ByVal edir As Integer) As Integer
        Dim distance As Integer = 0
        If xF > ef Then
            Select Case xdir
                Case 1 'up
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + xF
                        Case 0
                            distance = topFloor - ef + topFloor - xF
                    End Select
                Case 2
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + topFloor - btmFlr
                        Case 0
                            distance = topFloor - ef + topFloor - xF
                    End Select

            End Select
        Else
            Select Case xdir
                Case 1
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + xF - btmFlr
                        Case 0
                            distance = ef - btmFlr + xF - btmFlr
                    End Select
                Case 2
                    Select Case edir
                        Case 1
                            distance = 2 * topFloor - xF - ef
                        Case 2
                            distance = ef - xF
                        Case 0
                            distance = ef - btmFlr + xF - btmFlr
                    End Select

            End Select
        End If
        Return distance
    End Function
    Function nearElevator(ByVal xF As Integer, ByVal xdir As Integer, ByVal ef() As Integer, ByVal edir() As Integer) As Integer
        Dim min As Integer = 2 * topFloor
        Dim i As Integer
        For index = 0 To edir.Length - 1
            If min > elevatorDistance(xF, ef(index), xdir, edir(index)) Then
                min = elevatorDistance(xF, ef(index), xdir, edir(index))
                i = index
            End If
        Next
        Return i
    End Function
End Class

*

ranch vermin

  • Not much time left.
  • Replicant
  • ********
  • 643
  • Its nearly time!
Re: algorithms involving unknowns
« Reply #1 on: December 23, 2017, 09:16:23 pm »
I reckon its got a chance of working from a glance, but i call these methods, not algorythms.  to me an algorythm is a supervision function involving a score.

*

yotamarker

  • Trusty Member
  • ********
  • Replicant
  • *
  • 602
    • battle programming
Re: algorithms involving unknowns
« Reply #2 on: January 05, 2018, 04:36:16 pm »
this thread is about automatic programming. we are living in the age of low tech programming wise. when a programmer receives a task he has to develop an algorithm and then translate it into code with many in between steps. ignoring the aspect of human unemployment that is completely out of the scope of what this thread "cares" about, obviously said task should be delegated to the A.I which, in a technologically advanced reality, return a ready made alg + code/s.

having said that, the basic outline of my "plan" is to refer to algorithms as spells from a grimoire.
what do you think ?

*

yotamarker

  • Trusty Member
  • ********
  • Replicant
  • *
  • 602
    • battle programming
Re: algorithms involving unknowns
« Reply #3 on: January 09, 2018, 03:02:26 pm »
     *
    ***
   *****
  *******
 *********
***********
Code: [Select]
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {l1 = l1 + "*";}
System.out.println(l1);
l1 = "";
}

}
}
     *
    * *
   * * *
  * * * *
 * * * * *
Code: [Select]
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}

}
}
     *
    * *
   * * *
  * * * *
 * * * * *
 * * * * *
  * * * *
   * * *
    * *
     *
Code: [Select]
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}

//part 2 lower triangle :
for (int i=x;i>=1;i--){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code: [Select]
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//j = 0 evolve to j = 1
for(int j=1;j<i;j++) {l1 += j +" ";}
System.out.println(l1);
l1 = "";
}

}
}
       1
      2 1 2
    3 2 1 2 3
  4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Code: [Select]
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//mirror next for loop in reverse 3 of the for conditions
// evolve j=i to j = i-1
for(int j=0;j<(x-i);j++) {l1 = l1 + "  ";} // final adds this spacer, evolve " " to "  "
for(int j=i-1;j>1;j--) {l1 += j + " ";}
for(int j=1;j<i;j++) {l1 += j +" ";}
System.out.println(l1);
l1 = "";
}

}
}

*

yotamarker

  • Trusty Member
  • ********
  • Replicant
  • *
  • 602
    • battle programming
Re: algorithms involving unknowns
« Reply #4 on: January 28, 2018, 08:30:38 pm »
test of an example object class 8)
car class :

Code: [Select]
public class Car {
private int plateNumber, DailyCost;
private int rented=0;
private String type ; // the class variable will have private access modifiers
//to prevent code injection vulnerability
public int getPlateNumber() {
return plateNumber;
}
public void setPlateNumber(int plateNumber) {
this.plateNumber = plateNumber;
}
public int getDailyCost() {
return DailyCost;
}
public void setDailyCost(int dailyCost) {
DailyCost = dailyCost;
}
public int getRented() {
return rented;
}
public void setRented(int rented) {
this.rented = rented;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car() {} // empty constructor
public Car(int platenumber, String type, int DailyCost) {
this.plateNumber = platenumber;
this.type = type;
this.DailyCost = DailyCost;
}
public Car(int platenumber) {
this.plateNumber = platenumber;
}
public int bill(int Days) {
rented+=Days;
return DailyCost*Days;
}
public int totalBill() {
return DailyCost*rented;
}
public String toString() {

return this.plateNumber +"\nDaily cost : " + this.DailyCost +
"\ntype  : "+ this.type +"\nrented for: " + this.rented + " days\n\n\n";
}
}
MAIN:
Code: [Select]
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Car delorean = new Car(123, "DMC-12", 300);
Car peugeot = new Car();
Car mitsubishi = new Car(455);
System.out.println(delorean.toString());
System.out.println(peugeot.toString());
System.out.println(mitsubishi.toString());
System.out.println("bill "+delorean.bill(5));
System.out.println("bill "+delorean.bill(10));
System.out.println("total bill " + delorean.totalBill());
}

}

*

keghn

  • Trusty Member
  • *********
  • Terminator
  • *
  • 855
Re: algorithms involving unknowns
« Reply #5 on: February 01, 2018, 03:03:14 pm »

Dynamic Programming: