|
14 | 14 |
|
15 | 15 | from camel.agents import ChatAgent |
16 | 16 | from camel.configs import ChatGPTConfig |
17 | | -from camel.messages import BaseMessage |
18 | 17 | from camel.models import ModelFactory |
19 | 18 | from camel.types import ModelPlatformType, ModelType |
20 | 19 |
|
|
24 | 23 | model_config_dict=ChatGPTConfig(temperature=0.0).as_dict(), |
25 | 24 | ) |
26 | 25 |
|
27 | | -# Define system message |
28 | | -sys_msg = BaseMessage.make_assistant_message( |
29 | | - role_name="Assistant", |
30 | | - content="You are a helpful assistant.", |
31 | | -) |
32 | | - |
33 | 26 | # Set agent |
34 | | -camel_agent = ChatAgent(system_message=sys_msg, model=o1_model) |
| 27 | +camel_agent = ChatAgent(model=o1_model) |
35 | 28 |
|
36 | | -user_msg = BaseMessage.make_user_message( |
37 | | - role_name="User", |
38 | | - content="""Write a bash script that takes a matrix represented as a string |
| 29 | +# Set user message |
| 30 | +user_msg = """Write a bash script that takes a matrix represented as a string |
39 | 31 | with format '[1,2],[3,4],[5,6]' and prints the transpose in the same |
40 | | - format.""", |
41 | | -) |
| 32 | + format.""" |
42 | 33 |
|
43 | 34 | # Get response information |
44 | 35 | response = camel_agent.step(user_msg) |
45 | 36 | print(response.msgs[0].content) |
| 37 | +''' |
| 38 | +=============================================================================== |
| 39 | +Here's a bash script that transposes a matrix represented as the string format |
| 40 | +specified. It handles matrices of various sizes, including those with varying |
| 41 | +numbers of columns. |
| 42 | +
|
| 43 | +```bash |
| 44 | +#!/bin/bash |
| 45 | +
|
| 46 | +# Read input string from argument or stdin |
| 47 | +if [ -n "$1" ]; then |
| 48 | + input="$1" |
| 49 | +else |
| 50 | + read input |
| 51 | +fi |
| 52 | +
|
| 53 | +# Preprocess the input string to facilitate parsing |
| 54 | +# Replace '],[' with '];[' to use ';' as row separator |
| 55 | +input="${input//],[/];[}" |
| 56 | +
|
| 57 | +# Remove leading and trailing square brackets if any |
| 58 | +input="${input#[}" |
| 59 | +input="${input%]}" |
| 60 | +
|
| 61 | +# Split the input into rows |
| 62 | +IFS=';' read -ra rows <<< "$input" |
| 63 | +
|
| 64 | +declare -A matrix |
| 65 | +
|
| 66 | +nrows=${#rows[@]} |
| 67 | +ncols=0 |
| 68 | +
|
| 69 | +# Parse each row |
| 70 | +for ((i=0; i<nrows; i++)); do |
| 71 | + row="${rows[i]}" |
| 72 | + # Remove leading '[' and trailing ']' |
| 73 | + row="${row#[}" |
| 74 | + row="${row%]}" |
| 75 | + # Split row into elements |
| 76 | + IFS=',' read -ra elems <<< "$row" |
| 77 | + num_elems=${#elems[@]} |
| 78 | + if (( num_elems > ncols )); then |
| 79 | + ncols=$num_elems |
| 80 | + fi |
| 81 | + # Store elements in matrix associative array |
| 82 | + for ((j=0; j<num_elems; j++)); do |
| 83 | + matrix[$i,$j]="${elems[j]}" |
| 84 | + done |
| 85 | +done |
| 86 | +
|
| 87 | +# Function to join array elements with a delimiter |
| 88 | +join_by() { |
| 89 | + local d=$1; shift |
| 90 | + if [ "$#" -gt 0 ]; then |
| 91 | + printf %s "$1" "${@/#/$d}" |
| 92 | + fi |
| 93 | +} |
| 94 | +
|
| 95 | +# Now, build the transposed matrix |
| 96 | +transposed_rows=() |
| 97 | +for ((j=0; j<ncols; j++)); do |
| 98 | + tr_row_elements=() |
| 99 | + for ((i=0; i<nrows; i++)); do |
| 100 | + e="${matrix[$i,$j]:-}" # Use empty string if element is missing |
| 101 | + tr_row_elements+=("$e") |
| 102 | + done |
| 103 | + tr_row_elements_str=$(join_by ',' "${tr_row_elements[@]}") |
| 104 | + tr_row="[${tr_row_elements_str}]" |
| 105 | + transposed_rows+=("$tr_row") |
| 106 | +done |
| 107 | +
|
| 108 | +# Build output string |
| 109 | +output=$(join_by ',' "${transposed_rows[@]}") |
| 110 | +
|
| 111 | +# Print the output |
| 112 | +echo "$output" |
| 113 | +``` |
| 114 | +
|
| 115 | +**Usage:** |
| 116 | +
|
| 117 | +Save the script to a file, for example, `transpose_matrix.sh`, and make it |
| 118 | +executable: |
| 119 | +
|
| 120 | +```bash |
| 121 | +chmod +x transpose_matrix.sh |
| 122 | +``` |
| 123 | +
|
| 124 | +You can run the script by passing the matrix string as an argument: |
| 125 | +
|
| 126 | +```bash |
| 127 | +./transpose_matrix.sh '[1,2],[3,4],[5,6]' |
| 128 | +``` |
| 129 | +
|
| 130 | +Output: |
| 131 | +
|
| 132 | +``` |
| 133 | +[1,3,5],[2,4,6] |
| 134 | +``` |
| 135 | +
|
| 136 | +**Explanation:** |
| 137 | +
|
| 138 | +The script performs the following steps: |
| 139 | +
|
| 140 | +1. **Input Preprocessing:** |
| 141 | + - Replaces `],[` with `];[` to use `;` as a separator between rows. |
| 142 | + - Removes any leading or trailing square brackets. |
| 143 | +
|
| 144 | +2. **Parsing the Input into a Matrix:** |
| 145 | + - Splits the input string into rows using `IFS`. |
| 146 | + - For each row: |
| 147 | + - Removes the leading `[` and trailing `]`. |
| 148 | + - Splits the row into its elements. |
| 149 | + - Stores the elements into an associative array `matrix` with keys as |
| 150 | + `row,column`. |
| 151 | +
|
| 152 | +3. **Determining the Matrix Dimensions:** |
| 153 | + - Counts the number of rows (`nrows`). |
| 154 | + - Determines the maximum number of columns (`ncols`) across all rows. |
| 155 | +
|
| 156 | +4. **Transposing the Matrix:** |
| 157 | + - Iterates over each column index. |
| 158 | + - For each column, collects the elements from each row at that column index. |
| 159 | + - Builds the transposed row string and adds it to `transposed_rows`. |
| 160 | +
|
| 161 | +5. **Generating the Output:** |
| 162 | + - Joins the transposed rows using commas to form the final output string. |
| 163 | + - Prints the output. |
| 164 | +
|
| 165 | +**Notes:** |
| 166 | +
|
| 167 | +- The script supports matrices where rows have different numbers of columns. |
| 168 | +- Missing elements in the matrix (due to irregular column sizes) are handled |
| 169 | +by inserting empty strings in the transposed matrix. |
| 170 | +- The `join_by` function is used to handle joining array elements with a |
| 171 | +specified delimiter, ensuring proper formatting. |
| 172 | +=============================================================================== |
| 173 | +''' |
0 commit comments