Basic C programming help - c

Hey, I need help writing a simple program. I want to be able to demonstrate the use of integers and the remainders using a modulus. I'm stuck at how I should calculate the information. Any help would be much appreciated, but here's the general idea. The program encompasses the following:
1 week = 40 hours ($200 per week)
1 day = 7 hours ($45 per day)
($2 per hour)
Sample run:
Enter the total hours:59 (59 is just an example.)
You have:
1week
2day(s)
5hr(s)
Payment: $300.00
Here's what I've come up with so far...
int main(){
int totalWeekHrs = 0,
totalDayHrs = 0,
totalWorkedHrs = 0;
float totalPayment = 0,
payPerWeek = 0,
payPerDay = 0,
PayPerHr = 0;
// Input process
printf("Enter the total hours :");
scanf("%i",&totalWeekHrs,&totalDayHrs,&totalWorkedHrs);
// Calculative process
system("pause");
}

This smells like homework so I will explain how modulus works.
The modulus operator, %, performs integer division and returns the remainder. For example:
int foo = 6;
int bar = 4;
int remainder = foo % bar;
In that example, remainder will be set to 2.
You can read more about the modulus operator here.

I won't answer your question with code, since it seems homework. You have also stopped when you should really start coding!
The problem is another skin for the "change return" typical question. This is a eager algorithm which tries to resolve the objective with the biggest step it can take.
You have to have two paralell vectors:
{ 40, 7, 1 } // hours worked (week, day, hour)
{ 200, 45, 2 } // salary for each item above.
Notice that the first vector is sorted and that each position matches the same position in the second. The objective is 59 in your example.
For each position in the first vector, you have to divide by your objective, and annotate the remaining.
For example, for the first position, the biggest amount is 1, for the second, 2...
First step:
( 59 / 40 ) == 1
( 59 % 40 ) == 19
Second step:
( 19 / 7 ) == 2
( 19 % 7 ) == 5
Third step:
( 5 / 1 ) == 5
( 5 % 1 ) == 0
You'll finally get a vector as long as the first one with the results:
{ 1, 2, 5 } // one week, two days and five hours.
In order to show the results, just run over the vector, and multiply each position by the same position in the second vector:
1 week(s) ( 1 * 200 )
2 day(s) ( 2 * 45 )
5 hour(s) ( 5 * 2 )
( 1 * 200 ) + ( 2 * 45 ) + ( 5 * 2 ) = 300
This way you get the result you need.

Related

Can Someone please explain this Code to me

inputArray=[5, 1, 2, 3, 1, 4]
product = -1000
f = 0
for f in range(len(inputArray)):
try:
if product< inputArray[f] * inputArray[f+1]:
product = inputArray[f] * inputArray[f+1]
print product
except:
'no more'
print product
Result: 5,6
why doesn't it keep multiply the rest of the adjacent elements?
If you'd like that as an official answer, the explanation is below:
It does multiply on every iteration. It just doesn't print and redefine product unless product is less than the value of this iteration multiplied by next iteration. so visualize it like so:
-1000 < 5 so print. now the value of product is 5.
5 > 1 * 2 so do nothing.
5 < 2 * 3 so print. the value of product is now 6.
6 > 3 * 1 so do nothing.
6 > 1 * 4 so do nothing.
So you would have printed only 5 and 6.

Randoming Numbers is Way too Long [duplicate]

How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a solution?
Here is the way my own SuDoKu program does it:
Start with a complete, valid board (filled with 81 numbers).
Make a list of all 81 cell positions and shuffle it randomly.
As long as the list is not empty, take the next position from the list and remove the number from the related cell.
Test uniqueness using a fast backtracking solver. My solver is - in theory - able to count all solutions, but for testing uniqueness, it will stop immediately when it finds more than one solution.
If the current board has still just one solution, goto step 3) and repeat.
If the current board has more than one solution, undo the last removal (step 3), and continue step 3 with the next position from the list
Stop when you have tested all 81 positions.
This gives you not only unique boards, but boards where you cannot remove any more numbers without destroying the uniqueness of the solution.
Of course, this is only the second half of the algorithm. The first half is to find a complete valid board first (randomly filled!) It works very similar, but "in the other direction":
Start with an empty board.
Add a random number at one of the free cells (the cell is chosen randomly, and the number is chosen randomly from the list of numbers valid for this cell according to the SuDoKu rules).
Use the backtracking solver to check if the current board has at least one valid solution. If not, undo step 2 and repeat with another number and cell. Note that this step might produce full valid boards on its own, but those are in no way random.
Repeat until the board is completely filled with numbers.
Easy:
Find all solutions with an efficient backtracking algorithm.
If there is just one solution, you are done. Otherwise if you have more than one solution, find a position at which most of the solutions differ. Add the number at this position.
Go to 1.
I doubt you can find a solution that would be much faster than this.
You can cheat. Start with an existing Sudoku board that can be solved then fiddle with it.
You can swap any row of three 3x3 blocks with any other row. You can swap any column of three 3x3 blocks with another column. Within each block row or block column you can swap single rows and single columns. Finally you can permute the numbers so there are different numbers in the filled positions as long as the permutation is consistent across the whole board.
None of these changes will make a solvable board unsolvable.
Unless P = NP, there is no polynomial-time algorithm for generating general Sudoku problems with exactly one solution.
In his master's thesis, Takayuki Yato defined The Another Solution Problem (ASP), where the goal is, given a problem and some solution, to find a different solution to that problem or to show that none exists. Yato then defined ASP-completeness, problems for which it is difficult to find another solution, and showed that Sudoku is ASP-complete. Since he also proves that ASP-completeness implies NP-hardness, this means that if you allow for arbitrary-sized Sudoku boards, there is no polynomial-time algorithm to check if the puzzle you've generated has a unique solution (unless P = NP).
Sorry to spoil your hopes for a fast algorithm!
The solution is divide in to 2 parts:
A. Generating the number pattern 600 billion
B. Generating the masking pattern ~ 7e23 combinations
A ) For Number pattern the fastest way which can generate unique combinations with NO time spent on backtracing or testing
Step 1. Choose an already exiting matrix, I chose the below one as it can be made easily by human without any help from a computing device or solver:
First row is numbers in ascending order
Second row is also in ascending order but start from 4 & roll around
Third row is also in ascending order but start from 7 & roll around
Row 4,5,6: Replace the three cell column with the top right column - 2 5 8 and roll within the 3x3 cell for last column
Row 7,8,9: Replace the three cell column with the top right column - 3 6 9 and roll within the 3x3 cell for last column
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 1 5 6 4 8 9 7
5 6 4 8 9 7 2 3 1
8 9 7 2 3 1 5 6 4
3 1 2 6 4 5 9 7 8
6 4 5 9 7 8 3 1 2
9 7 8 3 1 2 6 4 5
Step 2. Shuffle the the digits and replace in all other cells
Step 3. Randomly rearrange columns 1,2 and 3 within themselves
Step 4. Randomly rearrange columns 4,5 and 6 within themselves
Step 5. Randomly rearrange columns 7,8 and 9 within themselves
Step 6. Randomly rearrange rows 1,2 and 3 within themselves
Step 7. Randomly rearrange rows 4,5 and 6 within themselves
Step 8. Randomly rearrange rows 7,8 and 9 within themselves
Step 9. Randomly rearrange in 3 column groups of size 9x3
Step 10. Randomly rearrange in 3 row groups of size 3x9
voila...
5 8 3 1 6 4 9 7 2
7 2 9 3 5 8 1 4 6
1 4 6 2 7 9 3 8 5
8 5 2 6 9 1 4 3 7
3 1 7 4 2 5 8 6 9
6 9 4 8 3 7 2 5 1
4 6 5 9 1 3 7 2 8
2 3 1 7 8 6 5 9 4
9 7 8 5 4 2 6 1 3
B ) For Masking Pattern we need to have a solver algorithm. As we already have a quite unique number grid (which is also solved!) this gives us faster performance for using solver
Step 1: Start with selecting 15 random locations out of the 81.
Step 2: Check with solver whether it has unique solution
Step 3: If solution not unique select additional location. iterate Steps 2 and 3 until unique solution found
This should give you the very unique and fast Sudoku board.
This way you can generate any possible sudoku board as well as any other nxn sudoku board
as for how efficient this algorithm is , it took 3.6 secs to generate a million boards in java & 3.5 sec in golang
Find any filled board of sudoku. (use trivial ones will not affect final result)
int[][] board = new int[][] {
{1,2,3, 4,5,6, 7,8,9},
{4,5,6, 7,8,9, 1,2,3},
{7,8,9, 1,2,3, 4,5,6},
{2,3,1, 5,6,4, 8,9,7},
{5,6,4, 8,9,7, 2,3,1},
{8,9,7, 2,3,1, 5,6,4},
{3,1,2, 6,4,5, 9,7,8},
{6,4,5, 9,7,8, 3,1,2},
{9,7,8, 3,1,2, 6,4,5}
};
for each number from 1 to 9 (say num), (i.e 1, 2, 3, 5, 6, 7, 8, 9) take a random number from range [1 to 9], traverse the board, swap num with your random number.
void shuffleNumbers() {
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(9);
swapNumbers(i, ranNum);
}
}
private void swapNumbers(int n1, int n2) {
for (int y = 0; y<9; y++) {
for (int x = 0; x<9; x++) {
if (board[x][y] == n1) {
board[x][y] = n2;
} else if (board[x][y] == n2) {
board[x][y] = n1;
}
}
}
}
Now shuffle rows. Take the first group of 3 rows , shuffle them , and do it for all rows. (in 9 X 9 sudoku), do it for second group and as well as third.
void shuffleRows() {
int blockNumber;
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(3);
blockNumber = i / 3;
swapRows(i, blockNumber * 3 + ranNum);
}
}
void swapRows(int r1, int r2) {
int[] row = board[r1];
board[r1] = board[r2];
board[r2] = row;
}
Swap columns, again take block of 3 columns , shuffle them, and do it for all 3 blocks
void shuffleCols() {
int blockNumber;
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(3);
blockNumber = i / 3;
swapCols(i, blockNumber * 3 + ranNum);
}
}
void swapCols(int c1, int c2) {
int colVal;
for (int i = 0; i < 9; i++){
colVal = board[i][c1];
board[i][c1] = board[i][c2];
board[i][c2] = colVal;
}
}
swap the row blocks itself (ie 3X9 blocks)
void shuffle3X3Rows() {
for (int i = 0; i < 3; i++) {
int ranNum = random.nextInt(3);
swap3X3Rows(i, ranNum);
}
}
void swap3X3Rows(int r1, int r2) {
for (int i = 0; i < 3; i++) {
swapRows(r1 * 3 + i, r2 * 3 + i);
}
}
do the same for columns, swap blockwise
void shuffle3X3Cols() {
for (int i = 0; i < 3; i++) {
int ranNum = random.nextInt(3);
swap3X3Cols(i, ranNum);
}
}
private void swap3X3Cols(int c1, int c2) {
for (int i = 0; i < 3; i++) {
swapCols(c1 * 3 + i, c2 * 3 + i);
}
}
Now you are done, your board should be a valid sudoku board
To generate a board with hidden values, this can be done using backtracking sudoku algorithm with it try to remove one element from the board until you have a board that is solvable, remove until it will become unsolvable even if you remove only one more element.
if you want to categorised final generated board by difficulty, just count how many numbers are left in board while removing element one by one. The less the number harder it will be to solve
least possible hints in sudoku can be 17, but all possible sudoku board not necessarily reducible to 17 hint sudoku
SWIFT 5 version
The simply way, here my code:
First, create the function into [[Int]] array
func getNumberSudoku() -> [[Int]] {
// Original number
let originalNum = [1,2,3,4,5,6,7,8,9]
// Create line 1 to 9 and shuffle from original
let line1 = originalNum.shuffled()
let line2 = line1.shift(withDistance: 3)
let line3 = line2.shift(withDistance: 3)
let line4 = line3.shift(withDistance: 1)
let line5 = line4.shift(withDistance: 3)
let line6 = line5.shift(withDistance: 3)
let line7 = line6.shift(withDistance: 1)
let line8 = line7.shift(withDistance: 3)
let line9 = line8.shift(withDistance: 3)
// Final array
let renewRow = [line1,line2,line3,line4,line5,line6,line7,line8,line9]
// Pre-shuffle for column
let colSh1 = [0,1,2].shuffled()
let colSh2 = [3,4,5].shuffled()
let colSh3 = [6,7,8].shuffled()
let rowSh1 = [0,1,2].shuffled()
let rowSh2 = [3,4,5].shuffled()
let rowSh3 = [6,7,8].shuffled()
// Create the let and var
let colResult = colSh1 + colSh2 + colSh3
let rowResult = rowSh1 + rowSh2 + rowSh3
var preCol: [Int] = []
var finalCol: [[Int]] = []
var prerow: [Int] = []
var finalRow: [[Int]] = []
// Shuffle the columns
for x in 0...8 {
preCol.removeAll()
for i in 0...8 {
preCol.append(renewRow[x][colResult[i]])
}
finalCol.append(preCol)
}
// Shuffle the rows
for x in 0...8 {
prerow.removeAll()
for i in 0...8 {
prerow.append(finalCol[x][rowResult[i]])
}
finalRow.append(prerow)
}
// Final, create the array into the [[Int]].
return finalRow
}
Then usage:
var finalArray = [[Int]]
finalArray = getNumberSudoku()
It's not easy to give a generic solution. You need to know a few things to generate a specific kind of Sudoku... for example, you cannot build a Sudoku with more than nine empty 9-number groups (rows, 3x3 blocks or columns). Minimum given numbers (i.e. "clues") in a single-solution Sudoku is believed to be 17, but number positions for this Sudoku are very specific if I'm not wrong. The average number of clues for a Sudoku is about 26, and I'm not sure but if you quit numbers of a completed grid until having 26 and leave those in a symmetric way, you may have a valid Sudoku.
On the other hand, you can just randomly quit numbers from completed grids and testing them with CHECKER or other tools until it comes up with an OK.
Here is a way to make a classic sudoku puzzle (sudoku puzzle with one and only solution; pre-filled squares are symmetrical around the center square R5C5).
1) start with a complete grid (using group filling plus circular shift to get it easily)
2) remove number(s) from two symmetrical squares if the cleared squares can be inferred using the remaining clues.
3) repeat (2) until all the numbers are checked.
Using this method you can create a very easy sudoku puzzle with or without programming. You can also use this method to craft harder Sudoku puzzles. You may want to search "create classic sudoku" on YouTube to have a step by step example.
You can start with any valid (filled) puzzle and modify it to produce a completely different one (again, filled). Instead of permutating groups of numbers, you can swap single cells - there will be no similarity whatsoever between the seed puzzle and the resulting puzzle. I have written a simple program long ago in VB, you can find it here: https://www.charalampakis.com/blog/programming-vb-net/a-simple-algorithm-for-creating-sudoku-puzzles-using-visual-basic. It can be translated to any language easily.
Then, randomly and gradually remove cells and check if the puzzle is solvable and has a unique solution. You can also rate the puzzle in terms of difficulty depending on the rules needed for the solution. Continue until removing any known cell leads to an unsolvable puzzle.
HTH
I also think that you will have to explicitly check uniqueness. If you have less than 17 givens, a unique solution is very unlikely, though: None has yet been found, although it is not clear yet whether it might exist.)
But you can also use a SAT-solver, as opposed to writing an own backtracking algorithm. That way, you can to some extent regulate how difficult it will be to find a solution: If you restrict the inference rules that the SAT-solver uses, you can check whether you can solve the puzzle easily. Just google for "SAT solving sudoku".
One way to generate sudoku faster.
find an exist sudoku.
exchange the value with a random group.
exchange the cell or the column or the row-grid or the column-grid.
You exchange the value will make the value different, if not exchange the rows or the column, the sudoku isn't changed in the essential.
You can flag the sudoku with 9 grids, the rows and column exchanged must do in the same grid. Like you can exchange row1-3, row4-6, row7-9, don't exchange row1-4 or row1-7. You can also exchange the row-grid(exchange row1~3 with the row4~6 or row7~9).
Solve the sudoku: record the empty with all the possible value, then check the value from 1 to 9. If one value is unique, remove it from the loop.
You may need code like this:
#pz is a 9x9 numpy array
def PossibleValueAtPosition(pz:[], row:int, col:int):
r=row//3*3
c=col//3*3
return {1,2,3,4,5,6,7,8,9}.difference(set(pz[r:r+3,c:c+3].flat)).difference(set(pz[row,:])).difference(set(pz[:,col]))
def SolvePuzzle(pz:[], n:int, Nof_solution:int):# init Nof_solution = 0
if Nof_solution>1:
return Nof_solution # no need to further check
if n>=81:
Nof_solution+=1
return Nof_solution
(row,col) = divmod(n,9)
if pz[row][col]>0: # location filled, try next location
Nof_solution = SolvePuzzle(pz, n+1, Nof_solution)
else:
l = PossibleValueAtPosition(pz, row,col)
for v in l: # if l = empty set, bypass all
pz[row][col] = v # try to fill a possible value v
Nof_solution = SolvePuzzle(pz, n+1, Nof_solution)
pz[row][col] = 0
return Nof_solution # resume the value, blacktrack
Quick and Dirty, but works:
import numpy as np
import math
N = 3
# rewrite of https://www.tutorialspoint.com/valid-sudoku-in-python
def isValidSudoku(M):
'''
Check a sudoku matrix:
A 9x9 sudoku matrix is valid iff every:
row contains 1 - 9 and
col contains 1 - 9 and
3x3 contains 1 - 9
0 is used for blank entry
'''
for i in range(9):
row = {}
col = {}
block = {}
row_cube = N * (i//N)
col_cube = N * (i%N)
for j in range(N*N):
if M[i][j] != 0 and M[i][j] in row:
return False
row[M[i][j]] = 1
if M[j][i] != 0 and M[j][i] in col:
return False
col[M[j][i]] = 1
rc = row_cube + j//N
cc = col_cube + j%N
if M[rc][cc] in block and M[rc][cc] != 0:
return False
block[M[rc][cc]]=1
return True
def generate_sudoku_puzzles(run_size, seed):
order = int(math.sqrt(run_size))
count = 0
valid = 0
empty = []
np.random.seed(seed) # for reproducible results
for k in range(order):
for l in range(order):
A = np.fromfunction(lambda i, j: ((k*i + l+j) % (N*N)) + 1, (N*N, N*N), dtype=int)
B = np.random.randint(2, size=(N*N, N*N))
empty.append(np.count_nonzero(B))
C = A*B
count += 1
if isValidSudoku(C):
valid += 1
last = C
# print('C(',k,l,') is valid sudoku:')
# print(C) # Uncomment for puzzle
print('Tried', count, 'valid', valid, 'yield', round(valid/count, 3)*100, '%', 'Average Clues', round(sum(empty)/len(empty)))
return(last)
posTest = np.array([(0, 7, 0, 0, 4, 0, 0, 6, 0), \
(3, 0, 0, 5, 0, 7, 0, 0, 2), \
(0, 0, 5, 0, 0, 0, 3, 0, 0), \
(0, 4, 0, 3, 0, 6, 0, 5, 0), \
(6, 0, 0, 0, 0, 0, 0, 0, 8), \
(0, 1, 0, 2, 0, 8, 0, 3, 0), \
(0, 0, 7, 0, 0, 0, 4, 0, 0), \
(1, 0, 0, 8, 0, 2, 0, 0, 9), \
(0, 6, 0, 0, 9, 0, 0, 1, 0), \
])
negTest = np.array([(0, 7, 0, 0, 4, 0, 0, 6, 2), \
(3, 0, 0, 5, 0, 7, 0, 0, 2), \
(0, 0, 5, 0, 0, 0, 3, 0, 0), \
(0, 4, 0, 3, 0, 6, 0, 5, 0), \
(6, 0, 0, 0, 0, 0, 0, 0, 8), \
(0, 1, 0, 2, 0, 8, 0, 3, 0), \
(0, 0, 7, 0, 0, 0, 4, 0, 0), \
(1, 0, 0, 8, 0, 2, 0, 0, 9), \
(0, 6, 0, 0, 9, 0, 0, 1, 0), \
])
print('Positive Quality Control Test:', isValidSudoku(posTest))
print('Negative Quality Control Test:', isValidSudoku(negTest))
print(generate_sudoku_puzzles(10000, 0))
Output:
Positive Quality Control Test: True
Negative Quality Control Test: False
Tried 10000 valid 31 yield 0.3 % Average Clues 40
[[0 0 2 3 0 0 0 7 8]
[7 8 9 1 2 0 0 0 0]
[5 0 0 0 9 0 0 3 0]
[0 0 0 6 7 8 0 0 2]
[0 2 0 0 0 0 7 8 9]
[8 0 0 2 3 0 0 0 0]
[0 0 0 0 0 2 3 0 5]
[0 5 6 0 8 9 1 2 0]
[0 3 0 5 0 0 0 9 0]]
Uncomment the two lines for puzzle source.
Here is a SQL Server stored procedure to generate Sudoku puzzles. I still need to remove values from the completed grid.
CREATE PROC dbo.sp_sudoku
AS
DROP TABLE IF EXISTS #cg
;
WITH cg
AS ( SELECT 1 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS g, 3 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 3 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 3 AS c, RAND() AS rnd)
, cg_seq
AS (SELECT g
, c
, row_number() over (partition by g
order by rnd) AS n
FROM cg)
SELECT g
, c + ((g - 1) * 3) AS c
, n + ((g - 1) * 3) AS n
INTO #cg
FROM cg_seq
--SELECT *
-- FROM #cg
-- ORDER BY g, c
DROP TABLE IF EXISTS #rg
;
WITH rg
AS ( SELECT 1 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 1 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 1 AS g, 3 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 3 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 3 AS r, RAND() AS rnd)
, rg_seq
AS (SELECT g
, r
, row_number() over (partition by g
order by rnd) AS n
FROM rg)
SELECT g
, r + ((g - 1) * 3) AS r
, n + ((g - 1) * 3) AS n
INTO #rg
FROM rg_seq
--SELECT *
-- FROM #rg
-- ORDER BY g, r
DROP TABLE IF EXISTS #r1
;
WITH r1
AS ( SELECT 1 AS r, 1 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 2 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 3 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 4 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 5 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 6 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 7 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 8 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 9 AS c, RAND() AS rnd)
, r1_seq
AS (SELECT r
, c
, row_number() over (order by rnd) AS n
FROM r1)
SELECT *
INTO #r1
FROM r1_seq
DROP TABLE IF EXISTS #r_tot
;
WITH r2
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM #r1)
, r3
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r2)
, r4
AS (SELECT r + 3 AS r
, CASE WHEN c = 1 THEN c + 8
ELSE c - 1
END AS c
, n
FROM #r1)
, r5
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r4)
, r6
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r5)
, r7
AS (SELECT r + 6 AS r
, CASE WHEN c = 1 THEN c + 7
WHEN c = 2 THEN c + 7
ELSE c - 2
END AS c
, n
FROM #r1)
, r8
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r7)
, r9
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r8)
, r_tot
AS (
SELECT * FROM #r1
UNION ALL SELECT * FROM r2
UNION ALL SELECT * FROM r3
UNION ALL SELECT * FROM r4
UNION ALL SELECT * FROM r5
UNION ALL SELECT * FROM r6
UNION ALL SELECT * FROM r7
UNION ALL SELECT * FROM r8
UNION ALL SELECT * FROM r9
)
SELECT *
INTO #r_tot
FROM r_tot
DROP TABLE IF EXISTS #r_tot2
;
SELECT g.n AS r
, r.c
, r.n
INTO #r_tot2
FROM #r_tot r
, #rg g
WHERE r.r = g.r
DROP TABLE IF EXISTS #c_tot2
;
SELECT r.r
, g.n AS c
, r.n
INTO #c_tot2
FROM #r_tot2 r
, #cg g
WHERE r.c = g.c
;
WITH c1 AS (SELECT r, n FROM #c_tot2 WHERE c = 1)
, c2 AS (SELECT r, n FROM #c_tot2 WHERE c = 2)
, c3 AS (SELECT r, n FROM #c_tot2 WHERE c = 3)
, c4 AS (SELECT r, n FROM #c_tot2 WHERE c = 4)
, c5 AS (SELECT r, n FROM #c_tot2 WHERE c = 5)
, c6 AS (SELECT r, n FROM #c_tot2 WHERE c = 6)
, c7 AS (SELECT r, n FROM #c_tot2 WHERE c = 7)
, c8 AS (SELECT r, n FROM #c_tot2 WHERE c = 8)
, c9 AS (SELECT r, n FROM #c_tot2 WHERE c = 9)
SELECT c1.r
, CAST(c1.n AS CHAR(2))
+ CAST(c2.n AS CHAR(2))
+ CAST(c3.n AS CHAR(2))
+ CAST(c4.n AS CHAR(2))
+ CAST(c5.n AS CHAR(2))
+ CAST(c6.n AS CHAR(2))
+ CAST(c7.n AS CHAR(2))
+ CAST(c8.n AS CHAR(2))
+ CAST(c9.n AS CHAR(2)) AS puzzle
FROM c1, c2, c3, c4, c5, c6, c7, c8, c9 WHERE c1.r = c2.r AND c3.r = c2.r AND c4.r = c3.r AND c5.r = c4.r AND c6.r = c5.r AND c7.r = c6.r AND c8.r = c7.r AND c9.r = c8.r
ORDER BY r

Find specific value's count in a vector

I generate a vector of 20 random integers in 1 : 6. For more clarity: d = floor ( 6 * rand ( 1 , 20 ) + 1). How can I count the numbers of sixes with MATLAB?
Just use this -
count = nnz(d==6)
One of the uses of nnz is to count the number of matches found. In this case, it would do comparisons between every element of d with 6 and return a logical array of ones or zeros based on the matches being found or not respectively and then nnz would count the number of occurrences of ones. nnz is really a very efficient tool for such cases, try exploring it.
nnz(d==6)
As given by Divakar is great. But using sum is usually faster:
sum(d(:)==6)
Example:
d = floor ( 6 * rand ( 1 , 2e6 ) + 1);
tic;nnz(d==6);toc;
tic;sum(d(:)==6);toc;
gives:
Elapsed time is 0.020109 seconds.
Elapsed time is 0.012709 seconds.
If you just want to count the number of occurrences of a single value then use nnz(d==6) as #Divakar suggested, but if you want to count the total number of multiple values, 3s and 6s say, you can do that with ismember:
num3s6s = nnz(ismember(d,[3 6]))
same as:
num3s6s = nnz(d==3 | d==6)
If you want to obtain the count of each value: use histc:
d = [1 2 4 2 3 4 5 4 3 6]; %// example data
values = 1:6; %// values you want the count of
count = histc(d, values);
This gives
count =
1 2 2 3 1 1

A case of a making-change (sort of). Find the minimal composition of weights

TLDR: (part 1) Need to print out the best composition of weights to reach a target weight. (part 2) Don't know what approach to choose. (part 3) Also, recursion is not my friend.
I am not asking for a solution, I am just looking for a direction.
PART 1
Some text first.
The input to the program is:
a number of weights
weights themselves
target weights I am supposed to compose
There always has to be a weight that = 1, so all the weights can be composed exactly.
I am supposed to print out the optimal composition of weights, for example
number of weights: 4
weights: 1, 3, 7, 10
target weight: 4
output: 2 x 7
PART 2
The first thing that came to my mind was the unbounded knapsack problem, where I would set all the values for weights to "1" and then I'd look for the lowest possible value in the knapsack. The problem is, my programming skills don't reach that level and my googling skills failed me when I wanted to find a fine article/code/video/whatever to understand it.
Then someone pointed out the making-change problem. The problem there is that it usually uses an array and I am expecting really large numbers, I cannot afford to alloc an array of size = target weight. Also, it seems to require quite a lot of magic if I want not only the lowest possible number of weights, but the exact counts.
My solution now, shall I?
sort the weights in descending order
count the number of weights yielded from the greedy algorithm
remove one biggest weight used and try to compose the weight without it
repeat 3 until I have removed all the "biggest weights" or the number of weights started to grow again
(for weights = 1, 3, 7, 10 and target = 14, greedy would give me 1 x 10 + 1 x 3 + 1 x 1, after the third step I would get (0 x 10 +) 2 x 7)
I got here. Only I need to repeat this not outside the recursive function (like I was doing until I realised it still doesn't give me the right results) but I need to move the loop into the recursive function.
PART 3
This is how parts of my code looks now:
for ( i = 0; i < weights_cnt; i ++ )
for ( j = 0; j <= weight / *(weights + i); j ++ )
{
*counter = 0;
if ( (res = greedyMagicImproved(weights + i, weight / *(weights + i) - j, weight, counter, min)) == 0 || min > *counter ) break;
else min = *counter;
}
It's a mess, I know. (the first recursive function I've ever written, sorry for that)
int greedyMagicImproved (int * weights, int limit, int weight, int * counter, int min)
{
if ( *counter > min ) return 0;
else if ( weight % *weights == 0 )
{
printf ("%d * %d\n", limit, *weights);
*counter += limit;
return *counter;
}
else if ( weight == 0 ) return *counter;
else if ( weight / *weights )
{
printf ("%d * %d + ", limit, *weights);
*counter += limit;
return (greedyMagicImproved(weights + 1, (weight - *weights * limit) / *(weights + 1), (weight - *weights * limit) % *weights, counter, min));
}
else return greedyMagicImproved(weights + 1, weight / *(weights + 1), weight, counter, min);
}
This one produces something like this:
Number of weights:
8
Actual weights of weights:
1 2 4 5 10 20 60 95
Weights to be composed:
124
GREEDY = 1 * 95 + 1 * 20 + 1 * 5 + 1 * 4
IMP = 1 * 95 + 1 * 20 + 1 * 5 + 1 * 4
2 * 60 + 1 * 4
6 * 20 + 1 * 4
... some more irrelevant results I'll deal with later
28
GREEDY = 1 * 20 + 1 * 5 + 1 * 2 + 1 * 1
IMP = 1 * 20 + 1 * 5 + 1 * 2 + 1 * 1
1 * 20 + 1 * 5 + 1 * 2 + 1 * 1
1 * 20 + 1 * 5 + 1 * 2 + 1 * 1
2 * 10 + 1 * 5 + 1 * 2 + 1 * 1
5 * 5 + 1 * 2 + 1 * 1
... some more results as well
While I get to see the correct result in the first case, I do not in the second.
So basically, my question is: Should I try to move the loop part into the recursion (and write it basically all over again because I have no idea how to do it) or should I go stealing/packing and making change?
Here is a DP formulation:
Let w[i], i=0,1,...p be the coin weights and f(t,i) be the number of coins needed to hit target t using only w[k], k >= i. When there is no possible way to make change, then f(t,i) is infinite. With this we know
f(t,i) = min_{n} [ n + f(t - n * w[i], i + 1) ]
In English, to hit the target with the minimum of coins using only coins i, i+1, ... p, choose all possible quantities n of coin i and then use the same DP to make change for the remaining amount using only coins i+1,i+2,..., finally choosing the solution that produced the minimum.
The base cases are common sense. For example f(0,_) = 0. You don't need any coins to hit a target of zero.
If T is the problem target, then the answer will be f(T,0).
Since you don't want the answer, I'll let you convert this to code. It's likely you'll get to answers faster if the weights are sorted in descending order.

matching two files contents using matlab and outputted the result as a matrix

I am new to Matlab. I need your help to solve my problem. I tried to implement several codes but unfortunately I could not be able to find the solution.
I have a file and I need to compare each row with the remaining and output the results as a matrix contains 1s of matched items and 0s otherwise. If two rows are overlapped using the 2 and 3 columns as shown in the example,the matrix has 1 . For example I have the following rows in the file.
1 X 10 20 A
2 Y 15 20 T
3 C 25 40 A
the output should be:
1 2 3
1 0 1 0
2 1 0 0
3 0 0 0
Really I would appreciate any help.
Thanks
That should do it
% open file
fh = fopen('test.txt');
% read data (adjust 10000 if necessary)
dat = textscan(fh, '%d %s %d %d %s', 10000);
% extract high and low values
hi = dat{4};
lo = dat{3};
% create grid
[ hig, log ] = meshgrid(hi, lo');
% compare high, low
overs = ~(log >= hig);
% check both ends & zero out diagonal
res = overs & overs' & ~logical(eye(length(hi)));
% close file
fclose(fh);
res

Resources