WeixiangYan/CodeTransOcean huggingface dataset:
Task: Associative array_Iteration



C#: using System;
using System.Collections.Generic;

namespace AssocArrays
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string,int> assocArray = new Dictionary<string,int>();

            assocArray["Hello"] = 1;
            assocArray.Add("World", 2);
            assocArray["!"] = 3;

            foreach (KeyValuePair<string, int> kvp in assocArray)
            {
                Console.WriteLine(kvp.Key + " : " + kvp.Value);
            }

            foreach (string key in assocArray.Keys)
            {
                Console.WriteLine(key);
            }

            foreach (int val in assocArray.Values)
            {
                Console.WriteLine(val.ToString());
            }
        }
    }
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration


C++: #include <iostream>
#include <map>
#include <string>

int main() {
  std::map<std::string, int> dict {
    {"One", 1},
    {"Two", 2},
    {"Three", 7}
  };

  dict["Three"] = 3;

  std::cout << "One: " << dict["One"] << std::endl;
  std::cout << "Key/Value pairs: " << std::endl;
  for(auto& kv: dict) {
    std::cout << "  " << kv.first << ": " << kv.second << std::endl;
  }

  return 0;
}










Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration




Go: myMap := map[string]int {
	   "hello": 13,
	   "world": 31,
	   "!"    : 71 }


for key, value := range myMap {
    fmt.Printf("key = %s, value = %d\n", key, value)
}


for key := range myMap {
    fmt.Printf("key = %s\n", key)
}


for _, value := range myMap {
    fmt.Printf("value = %d\n", value)
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration





Java: Map<String, Integer> map = new HashMap<String, Integer>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);


for (Map.Entry<String, Integer> e : map.entrySet()) {
    String key = e.getKey();
    Integer value = e.getValue();
    System.out.println("key = " + key + ", value = " + value);
}


for (String key : map.keySet()) {
    System.out.println("key = " + key);
}


for (Integer value : map.values()) {
    System.out.println("value = " + value);
}







Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration







Python: myDict = { "hello": 13,
	   "world": 31,
	   "!"    : 71 }


for key, value in myDict.items():
    print ("key = %s, value = %s" % (key, value))


for key in myDict:
    print ("key = %s" % key)

for key in myDict.keys():
    print ("key = %s" % key)


for value in myDict.values():
    print ("value = %s" % value)





Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration








VB: Option Explicit
Sub Test()
    Dim h As Object, i As Long, u, v, s
    Set h = CreateObject("Scripting.Dictionary")
    h.Add "A", 1
    h.Add "B", 2
    h.Add "C", 3

    
    For Each s In h.Keys
        Debug.Print s
    Next

    
    For Each s In h.Items
        Debug.Print s
    Next

    
    u = h.Keys
    v = h.Items
    For i = 0 To h.Count - 1
        Debug.Print u(i), v(i)
    Next
End Sub




Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: Associative array_Iteration



C#: using System;
using System.Collections.Generic;

namespace AssocArrays
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string,int> assocArray = new Dictionary<string,int>();

            assocArray["Hello"] = 1;
            assocArray.Add("World", 2);
            assocArray["!"] = 3;

            foreach (KeyValuePair<string, int> kvp in assocArray)
            {
                Console.WriteLine(kvp.Key + " : " + kvp.Value);
            }

            foreach (string key in assocArray.Keys)
            {
                Console.WriteLine(key);
            }

            foreach (int val in assocArray.Values)
            {
                Console.WriteLine(val.ToString());
            }
        }
    }
}
Task: Associative array_Iteration


C++: #include <iostream>
#include <map>
#include <string>

int main() {
  std::map<std::string, int> dict {
    {"One", 1},
    {"Two", 2},
    {"Three", 7}
  };

  dict["Three"] = 3;

  std::cout << "One: " << dict["One"] << std::endl;
  std::cout << "Key/Value pairs: " << std::endl;
  for(auto& kv: dict) {
    std::cout << "  " << kv.first << ": " << kv.second << std::endl;
  }

  return 0;
}
Task: Associative array_Iteration





Java: Map<String, Integer> map = new HashMap<String, Integer>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);


for (Map.Entry<String, Integer> e : map.entrySet()) {
    String key = e.getKey();
    Integer value = e.getValue();
    System.out.println("key = " + key + ", value = " + value);
}


for (String key : map.keySet()) {
    System.out.println("key = " + key);
}


for (Integer value : map.values()) {
    System.out.println("value = " + value);
}
Task: Associative array_Iteration







Python: myDict = { "hello": 13,
	   "world": 31,
	   "!"    : 71 }


for key, value in myDict.items():
    print ("key = %s, value = %s" % (key, value))


for key in myDict:
    print ("key = %s" % key)

for key in myDict.keys():
    print ("key = %s" % key)


for value in myDict.values():
    print ("value = %s" % value)
Task: Associative array_Iteration








VB: Option Explicit
Sub Test()
    Dim h As Object, i As Long, u, v, s
    Set h = CreateObject("Scripting.Dictionary")
    h.Add "A", 1
    h.Add "B", 2
    h.Add "C", 3

    
    For Each s In h.Keys
        Debug.Print s
    Next

    
    For Each s In h.Items
        Debug.Print s
    Next

    
    u = h.Keys
    v = h.Items
    For i = 0 To h.Count - 1
        Debug.Print u(i), v(i)
    Next
End Sub
Task: Associative array_Iteration




Go: myMap := map[string]int {
	   "hello": 13,
	   "world": 31,
	   "!"    : 71 }


for key, value := range myMap {
    fmt.Printf("key = %s, value = %d\n", key, value)
}


for key := range myMap {
    fmt.Printf("key = %s\n", key)
}


for _, value := range myMap {
    fmt.Printf("value = %d\n", value)
}
Task: Associative array_Iteration






PHP: <?php
$pairs = array( "hello" => 1,
		"world" => 2,
		"!"     => 3 );

foreach($pairs as $k => $v) {
  echo "(k,v) = ($k, $v)\n";
}

foreach(array_keys($pairs) as $key) {
  echo "key = $key, value = $pairs[$key]\n";
}

foreach($pairs as $value) {
  echo "values = $value\n";
}
?>
Task: Associative array_Iteration






PHP: <?php
$pairs = array( "hello" => 1,
		"world" => 2,
		"!"     => 3 );

foreach($pairs as $k => $v) {
  echo "(k,v) = ($k, $v)\n";
}

foreach(array_keys($pairs) as $key) {
  echo "key = $key, value = $pairs[$key]\n";
}

foreach($pairs as $value) {
  echo "values = $value\n";
}
?>






Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Task: GUI_Maximum window dimensions



C#: using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        Rectangle bounds = Screen.PrimaryScreen.Bounds;
        Console.WriteLine($"Primary screen bounds:  {bounds.Width}x{bounds.Height}");

        Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
        Console.WriteLine($"Primary screen working area:  {workingArea.Width}x{workingArea.Height}");
    }
}









Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions

C: #include<windows.h>
#include<stdio.h>

int main()
{
	printf("Dimensions of the screen are (w x h) : %d x %d pixels",GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
	return 0;
}











Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions




Go: package main

import (
    "fmt"
    "github.com/go-vgo/robotgo"
)

func main() {
    w, h := robotgo.GetScreenSize()
    fmt.Printf("Screen size: %d x %d\n", w, h)
    fpid, err := robotgo.FindIds("firefox")
    if err == nil && len(fpid) > 0 {
        pid := fpid[0]
        robotgo.ActivePID(pid)
        robotgo.MaxWindow(pid)
        _, _, w, h = robotgo.GetBounds(pid)
        fmt.Printf("Max usable : %d x %d\n", w, h)
    }
}








Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions





Java: import java.awt.*;
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }

    Test() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        Dimension screenSize = toolkit.getScreenSize();
        System.out.println("Physical screen size: " + screenSize);

        Insets insets = toolkit.getScreenInsets(getGraphicsConfiguration());
        System.out.println("Insets: " + insets);

        screenSize.width -= (insets.left + insets.right);
        screenSize.height -= (insets.top + insets.bottom);
        System.out.println("Max available: " + screenSize);
    }
}







Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions







Python: 

import tkinter as tk 

root = tk.Tk() 
root.state('zoomed') 
root.update_idletasks() 
tk.Label(root, text=(str(root.winfo_width())+ " x " +str(root.winfo_height())),
         font=("Helvetica", 25)).pack() 
root.mainloop()





Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions








VB: TYPE syswindowstru
  screenheight AS INTEGER
  screenwidth AS INTEGER
  maxheight AS INTEGER
  maxwidth AS INTEGER
END TYPE

DIM syswindow AS syswindowstru



syswindow.screenwidth = Screen.Width / Screen.TwipsPerPixelX
syswindow.screenheight=Screen.Height / Screen.TwipsPerPixelY






Ada: with Gtk.Main;
with Glib;
with Gtk.Window;  use Gtk.Window;
with Gtk.Enums;   use Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;

procedure Max_Size is

   Win          : Gtk_Window;
   Win_W, Win_H : Glib.Gint;
   package Int_Io is new Integer_IO (Glib.Gint);
   Hid : Gtk.Main.Quit_Handler_Id;

begin
   Gtk.Main.Init;
   Gtk_New (Win);
   Initialize (Win, Window_Toplevel);
   Maximize (Win);
   Show (Win);
   Get_Size (Win, Win_W, Win_H);
   Put ("Maximum dimensions of window : W ");
   Int_Io.Put (Win_W, Width => 4);
   Put (" x H ");
   Int_Io.Put (Win_H, Width => 4);
   New_Line;
   Hid := Gtk.Main.Quit_Add_Destroy (0, Win);
end Max_Size;
Task: GUI_Maximum window dimensions

C: #include<windows.h>
#include<stdio.h>

int main()
{
	printf("Dimensions of the screen are (w x h) : %d x %d pixels",GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
	return 0;
}
Task: GUI_Maximum window dimensions



C#: using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        Rectangle bounds = Screen.PrimaryScreen.Bounds;
        Console.WriteLine($"Primary screen bounds:  {bounds.Width}x{bounds.Height}");

        Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
        Console.WriteLine($"Primary screen working area:  {workingArea.Width}x{workingArea.Height}");
    }
}
Task: GUI_Maximum window dimensions





Java: import java.awt.*;
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }

    Test() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        Dimension screenSize = toolkit.getScreenSize();
        System.out.println("Physical screen size: " + screenSize);

        Insets insets = toolkit.getScreenInsets(getGraphicsConfiguration());
        System.out.println("Insets: " + insets);

        screenSize.width -= (insets.left + insets.right);
        screenSize.height -= (insets.top + insets.bottom);
        System.out.println("Max available: " + screenSize);
    }
}
Task: GUI_Maximum window dimensions







Python: 

import tkinter as tk 

root = tk.Tk() 
root.state('zoomed') 
root.update_idletasks() 
tk.Label(root, text=(str(root.winfo_width())+ " x " +str(root.winfo_height())),
         font=("Helvetica", 25)).pack() 
root.mainloop()
Task: GUI_Maximum window dimensions








VB: TYPE syswindowstru
  screenheight AS INTEGER
  screenwidth AS INTEGER
  maxheight AS INTEGER
  maxwidth AS INTEGER
END TYPE

DIM syswindow AS syswindowstru



syswindow.screenwidth = Screen.Width / Screen.TwipsPerPixelX
syswindow.screenheight=Screen.Height / Screen.TwipsPerPixelY
Task: GUI_Maximum window dimensions




Go: package main

import (
    "fmt"
    "github.com/go-vgo/robotgo"
)

func main() {
    w, h := robotgo.GetScreenSize()
    fmt.Printf("Screen size: %d x %d\n", w, h)
    fpid, err := robotgo.FindIds("firefox")
    if err == nil && len(fpid) > 0 {
        pid := fpid[0]
        robotgo.ActivePID(pid)
        robotgo.MaxWindow(pid)
        _, _, w, h = robotgo.GetBounds(pid)
        fmt.Printf("Max usable : %d x %d\n", w, h)
    }
}
Task: Knapsack problem_0-1

C: #include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *name;
    int weight;
    int value;
} item_t;

item_t items[] = {
    {"map",                      9,   150},
    {"compass",                 13,    35},
    {"water",                  153,   200},
    {"sandwich",                50,   160},
    {"glucose",                 15,    60},
    {"tin",                     68,    45},
    {"banana",                  27,    60},
    {"apple",                   39,    40},
    {"cheese",                  23,    30},
    {"beer",                    52,    10},
    {"suntan cream",            11,    70},
    {"camera",                  32,    30},
    {"T-shirt",                 24,    15},
    {"trousers",                48,    10},
    {"umbrella",                73,    40},
    {"waterproof trousers",     42,    70},
    {"waterproof overclothes",  43,    75},
    {"note-case",               22,    80},
    {"sunglasses",               7,    20},
    {"towel",                   18,    12},
    {"socks",                    4,    50},
    {"book",                    30,    10},
};

int *knapsack (item_t *items, int n, int w) {
    int i, j, a, b, *mm, **m, *s;
    mm = calloc((n + 1) * (w + 1), sizeof (int));
    m = malloc((n + 1) * sizeof (int *));
    m[0] = mm;
    for (i = 1; i <= n; i++) {
        m[i] = &mm[i * (w + 1)];
        for (j = 0; j <= w; j++) {
            if (items[i - 1].weight > j) {
                m[i][j] = m[i - 1][j];
            }
            else {
                a = m[i - 1][j];
                b = m[i - 1][j - items[i - 1].weight] + items[i - 1].value;
                m[i][j] = a > b ? a : b;
            }
        }
    }
    s = calloc(n, sizeof (int));
    for (i = n, j = w; i > 0; i--) {
        if (m[i][j] > m[i - 1][j]) {
            s[i - 1] = 1;
            j -= items[i - 1].weight;
        }
    }
    free(mm);
    free(m);
    return s;
}

int main () {
    int i, n, tw = 0, tv = 0, *s;
    n = sizeof (items) / sizeof (item_t);
    s = knapsack(items, n, 400);
    for (i = 0; i < n; i++) {
        if (s[i]) {
            printf("%-22s %5d %5d\n", items[i].name, items[i].weight, items[i].value);
            tw += items[i].weight;
            tv += items[i].value;
        }
    }
    printf("%-22s %5d %5d\n", "totals:", tw, tv);
    return 0;
}
Task: Knapsack problem_0-1





Java: package hu.pj.alg.test;

import hu.pj.alg.ZeroOneKnapsack;
import hu.pj.obj.Item;
import java.util.*;
import java.text.*;

public class ZeroOneKnapsackForTourists {

    public ZeroOneKnapsackForTourists() {
        ZeroOneKnapsack zok = new ZeroOneKnapsack(400); 

        
        zok.add("map", 9, 150);
        zok.add("compass", 13, 35);
        zok.add("water", 153, 200);
        zok.add("sandwich", 50, 160);
        zok.add("glucose", 15, 60);
        zok.add("tin", 68, 45);
        zok.add("banana", 27, 60);
        zok.add("apple", 39, 40);
        zok.add("cheese", 23, 30);
        zok.add("beer", 52, 10);
        zok.add("suntan cream", 11, 70);
        zok.add("camera", 32, 30);
        zok.add("t-shirt", 24, 15);
        zok.add("trousers", 48, 10);
        zok.add("umbrella", 73, 40);
        zok.add("waterproof trousers", 42, 70);
        zok.add("waterproof overclothes", 43, 75);
        zok.add("note-case", 22, 80);
        zok.add("sunglasses", 7, 20);
        zok.add("towel", 18, 12);
        zok.add("socks", 4, 50);
        zok.add("book", 30, 10);

        
        List<Item> itemList = zok.calcSolution();

        
        if (zok.isCalculated()) {
            NumberFormat nf  = NumberFormat.getInstance();

            System.out.println(
                "Maximal weight           = " +
                nf.format(zok.getMaxWeight() / 100.0) + " kg"
            );
            System.out.println(
                "Total weight of solution = " +
                nf.format(zok.getSolutionWeight() / 100.0) + " kg"
            );
            System.out.println(
                "Total value              = " +
                zok.getProfit()
            );
            System.out.println();
            System.out.println(
                "You can carry the following materials " +
                "in the knapsack:"
            );
            for (Item item : itemList) {
                if (item.getInKnapsack() == 1) {
                    System.out.format(
                        "%1$-23s %2$-3s %3$-5s %4$-15s \n",
                        item.getName(),
                        item.getWeight(), "dag  ",
                        "(value = " + item.getValue() + ")"
                    );
                }
            }
        } else {
            System.out.println(
                "The problem is not solved. " +
                "Maybe you gave wrong data."
            );
        }

    }

    public static void main(String[] args) {
        new ZeroOneKnapsackForTourists();
    }

}